如何将集合转换为列表? [英] How to convert a Collection to List?
问题描述
我在中使用 TreeBidiMap
Apache Collections 库。我想对双倍
的值进行排序。
I am using TreeBidiMap
from the Apache Collections library. I want to sort this on the values which are doubles
.
我的方法是检索使用以下值的集合
:
Collection coll = themap.values();
哪种情况自然有效。
主要问题:我现在想知道如何将 coll
转换/转换(不确定哪个是正确的)列表
所以它可以排序吗?
Main Question: I now want to know how I can convert/cast (not sure which is correct) coll
into a List
so it can be sorted?
然后我打算迭代已排序的 List
对象,该对象应该按顺序从中获取相应的密钥使用 themap.getKey(iterator.next())
迭代器将位于 TreeBidiMap
( themap
)双精度
列表中。
I then intend to iterate over the sorted List
object, which should be in order and get the appropriate keys from the TreeBidiMap
(themap
) using themap.getKey(iterator.next())
where the iterator will be over the list of doubles
.
推荐答案
List list = new ArrayList(coll);
Collections.sort(list);
正如Erel Segal Halevi所说,如果coll已经是一个列表,你可以跳过第一步。但这取决于TreeBidiMap的内部。
As Erel Segal Halevi says below, if coll is already a list, you can skip step one. But that would depend on the internals of TreeBidiMap.
List list;
if (coll instanceof List)
list = (List)coll;
else
list = new ArrayList(coll);
这篇关于如何将集合转换为列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!