如何将集合转换为列表?

Ank*_*kur 277 java sorting collections list apache-commons-collection

我使用TreeBidiMap来自Apache的集合库.我想对这些值进行排序doubles.

我的方法是Collection使用以下方法检索一个值:

Collection coll = themap.values();
Run Code Online (Sandbox Code Playgroud)

这自然很好.

主要问题:我现在想知道如何转换/转换(不确定哪个是正确的)coll到一个List可以排序?

然后我打算遍历排序的List对象,该对象应按顺序从TreeBidiMap(themap)中使用themap.getKey(iterator.next())迭代器将在列表上的位置获取相应的键doubles.

Pau*_*lin 453

List list = new ArrayList(coll);
Collections.sort(list);
Run Code Online (Sandbox Code Playgroud)

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

List list;
if (coll instanceof List)
  list = (List)coll;
else
  list = new ArrayList(coll);
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这两种方法有不同的副作用:将集合转换为列表,然后排序也将对原始集合进行排序; 创建副本不会. (3认同)

Jac*_*eow 90

这样的东西应该工作,调用带有Collection 的ArrayList构造函数:

List theList = new ArrayList(coll);
Run Code Online (Sandbox Code Playgroud)


Ere*_*evi 33

我认为Paul Tomblin的答案可能是浪费,以防coll已经是一个列表,因为它将创建一个新列表并复制所有元素.如果coll包含许多元素,这可能需要很长时间.

我的建议是:

List list;
if (coll instanceof List)
  list = (List)coll;
else
  list = new ArrayList(coll);
Collections.sort(list);
Run Code Online (Sandbox Code Playgroud)


Eya*_*fri 19

我相信你可以这样写:

coll.stream().collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)


Ani*_*wat 11

Java 10 引入了List#copyOf它在保留顺序的同时返回不可修改的列表

List<Integer> list = List.copyOf(coll);
Run Code Online (Sandbox Code Playgroud)


Osc*_*Ryz 7

Collections.sort( new ArrayList( coll ) );
Run Code Online (Sandbox Code Playgroud)


hba*_*ara 6

Java 8 以后...

您可以使用StreamsCollectors.toCollection()将 Collection 转换为任何集合(即 List、Set 和 Queue)。

考虑以下示例地图

Map<Integer, Double> map = Map.of(
    1, 1015.45,
    2, 8956.31,
    3, 1234.86,
    4, 2348.26,
    5, 7351.03
);
Run Code Online (Sandbox Code Playgroud)

到数组列表

List<Double> arrayList = map.values()
                            .stream()
                            .collect(
                                Collectors.toCollection(ArrayList::new)
                            );
Run Code Online (Sandbox Code Playgroud)

输出:[7351.03、2348.26、1234.86、8956.31、1015.45]

到已排序的ArrayList(升序)

List<Double> arrayListSortedAsc = map.values()
                                        .stream()
                                        .sorted()
                                        .collect(
                                            Collectors.toCollection(ArrayList::new)
                                        );
Run Code Online (Sandbox Code Playgroud)

输出:[1015.45、1234.86、2348.26、7351.03、8956.31]

到已排序的ArrayList(降序)

List<Double> arrayListSortedDesc = map.values()
                                        .stream()
                                        .sorted(
                                            (a, b) -> b.compareTo(a)
                                        )
                                        .collect(
                                            Collectors.toCollection(ArrayList::new)
                                        );
Run Code Online (Sandbox Code Playgroud)

输出:[8956.31、7351.03、2348.26、1234.86、1015.45]

到链表

List<Double> linkedList = map.values()
                                .stream()
                                .collect(
                                    Collectors.toCollection(LinkedList::new)
                                );
Run Code Online (Sandbox Code Playgroud)

输出:[7351.03、2348.26、1234.86、8956.31、1015.45]

到哈希集

Map<Integer, Double> map = Map.of(
    1, 1015.45,
    2, 8956.31,
    3, 1234.86,
    4, 2348.26,
    5, 7351.03
);
Run Code Online (Sandbox Code Playgroud)

输出:[2348.26、8956.31、1015.45、1234.86、7351.03]

到优先队列

PriorityQueue<Double> priorityQueue = map.values()
                                            .stream()
                                            .collect(
                                                Collectors.toCollection(PriorityQueue::new)
                                            );
Run Code Online (Sandbox Code Playgroud)

输出:[1015.45、1234.86、2348.26、8956.31、7351.03]

参考

Java - 包 java.util.stream

Java - 包 java.util