我正在使用来自Apache集合库的TreeBidiMap。我想对double类型的值进行排序。

我的方法是检索值的集合使用:

Collection coll = themap.values();

这很正常。

主要问题:我现在想知道我如何转换/cast(不确定哪个是正确的)coll成一个列表,这样它就可以排序?

然后,我打算遍历排序的List对象,它应该是有序的,并使用themap. getkey (iterator.next())从TreeBidiMap (themap)中获得适当的键,其中迭代器将在double列表上。


当前回答

下面是一个次优解决方案:

Collections.list(Collections.enumeration(coll));

其他回答

Collections.sort( new ArrayList( coll ) );
List list = new ArrayList(coll);
Collections.sort(list);

正如Erel Segal Halevi在下面所说的,如果coll已经是一个列表,您可以跳过第一步。但这取决于TreeBidiMap的内部结构。

List list;
if (coll instanceof List)
  list = (List)coll;
else
  list = new ArrayList(coll);

下面是一个次优解决方案:

Collections.list(Collections.enumeration(coll));

类似这样的方法应该可以工作,调用ArrayList构造函数来接受一个Collection:

List theList = new ArrayList(coll);

Java 10引入了List#copyOf,返回不可修改的List,同时保留顺序:

List<Integer> list = List.copyOf(coll);