Java 8 aufwärts...
Sie können Collection in jede beliebige Collection (d.h. List, Set und Queue) konvertieren, indem Sie Ströme y Collectors.toCollection() .
Betrachten Sie die folgende Beispielkarte
Map<Integer, Double> map = Map.of(
1, 1015.45,
2, 8956.31,
3, 1234.86,
4, 2348.26,
5, 7351.03
);
zu ArrayList
List<Double> arrayList = map.values()
.stream()
.collect(
Collectors.toCollection(ArrayList::new)
);
Ausgabe: [7351.03, 2348.26, 1234.86, 8956.31, 1015.45]
in sortierte ArrayListe (aufsteigende Reihenfolge)
List<Double> arrayListSortedAsc = map.values()
.stream()
.sorted()
.collect(
Collectors.toCollection(ArrayList::new)
);
Ausgabe: [1015.45, 1234.86, 2348.26, 7351.03, 8956.31]
zu Sortierte ArrayListe (absteigende Reihenfolge)
List<Double> arrayListSortedDesc = map.values()
.stream()
.sorted(
(a, b) -> b.compareTo(a)
)
.collect(
Collectors.toCollection(ArrayList::new)
);
Ausgabe: [8956.31, 7351.03, 2348.26, 1234.86, 1015.45]
zu LinkedList
List<Double> linkedList = map.values()
.stream()
.collect(
Collectors.toCollection(LinkedList::new)
);
Ausgabe: [7351.03, 2348.26, 1234.86, 8956.31, 1015.45]
zu HashSet
Set<Double> hashSet = map.values()
.stream()
.collect(
Collectors.toCollection(HashSet::new)
);
Ausgabe: [2348.26, 8956.31, 1015.45, 1234.86, 7351.03]
zu PriorityQueue
PriorityQueue<Double> priorityQueue = map.values()
.stream()
.collect(
Collectors.toCollection(PriorityQueue::new)
);
Ausgabe: [1015.45, 1234.86, 2348.26, 8956.31, 7351.03]
Referenz
Java - Paket java.util.stream
Java - Paket java.util