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

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

问题描述

我使用 Apache Collections 库中的 TreeBidiMap 。我想对 doubles 的值排序。

I am using TreeBidiMap from the Apache Collections library. I want to sort this on the values which are doubles.

我的方法是检索 Collection 的值使用:

集合coll = themap.values();
$ b $

主要问题:我现在想知道如何转换/投射(不确定是否正确) coll code>转换为列表以便可以排序?

My method is to retrieve a Collection of the values using:
Collection coll = themap.values();
Which naturally works fine.
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 code>其中迭代器将在 doubles 的列表上。

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);

这篇关于如何将集合转换为列表中的JAVA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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