如何将集合转换为列表? [英] How to convert a Collection to List?

查看:2893
本文介绍了如何将集合转换为列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在中使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆