How to convert a Collection to List?

Viewed 601521

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

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?

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.

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

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

Something like this should work, calling the ArrayList constructor that takes a Collection:

List theList = new ArrayList(coll);

Java 10 introduced List#copyOf which returns unmodifiable List while preserving the order:

List<Integer> list = List.copyOf(coll);
Collections.sort( new ArrayList( coll ) );

Java 8 onwards...

You can convert Collection to any collection (i.e, List, Set, and Queue) using Streams and Collectors.toCollection().

Consider the following example map

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

to ArrayList

List<Double> arrayList = map.values()
                            .stream()
                            .collect(
                                Collectors.toCollection(ArrayList::new)
                            );

Output: [7351.03, 2348.26, 1234.86, 8956.31, 1015.45]

to Sorted ArrayList (Ascending order)

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

Output: [1015.45, 1234.86, 2348.26, 7351.03, 8956.31]

to Sorted ArrayList (Descending order)

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

Output: [8956.31, 7351.03, 2348.26, 1234.86, 1015.45]

to LinkedList

List<Double> linkedList = map.values()
                                .stream()
                                .collect(
                                    Collectors.toCollection(LinkedList::new)
                                );

Output: [7351.03, 2348.26, 1234.86, 8956.31, 1015.45]

to HashSet

Set<Double> hashSet = map.values()
                            .stream()
                            .collect(
                                Collectors.toCollection(HashSet::new)
                            );

Output: [2348.26, 8956.31, 1015.45, 1234.86, 7351.03]

to PriorityQueue

PriorityQueue<Double> priorityQueue = map.values()
                                            .stream()
                                            .collect(
                                                Collectors.toCollection(PriorityQueue::new)
                                            );

Output: [1015.45, 1234.86, 2348.26, 8956.31, 7351.03]

Reference

Java - Package java.util.stream

Java - Package java.util

Use streams:

someCollection.stream().collect(Collectors.toList())
Related