Java 8开始…
您可以使用Streams和collections . 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
);
对数组列表
List<Double> arrayList = map.values()
.stream()
.collect(
Collectors.toCollection(ArrayList::new)
);
输出:[7351.03,2348.26,1234.86,8956.31,1015.45]
排序数组列表(升序)
List<Double> arrayListSortedAsc = map.values()
.stream()
.sorted()
.collect(
Collectors.toCollection(ArrayList::new)
);
输出:[1015.45,1234.86,2348.26,7351.03,8956.31]
排序数组列表(降序)
List<Double> arrayListSortedDesc = map.values()
.stream()
.sorted(
(a, b) -> b.compareTo(a)
)
.collect(
Collectors.toCollection(ArrayList::new)
);
输出:[8956.31,7351.03,2348.26,1234.86,1015.45]
到链表
List<Double> linkedList = map.values()
.stream()
.collect(
Collectors.toCollection(LinkedList::new)
);
输出:[7351.03,2348.26,1234.86,8956.31,1015.45]
对HashSet
Set<Double> hashSet = map.values()
.stream()
.collect(
Collectors.toCollection(HashSet::new)
);
输出:[2348.26,8956.31,1015.45,1234.86,7351.03]
对PriorityQueue
PriorityQueue<Double> priorityQueue = map.values()
.stream()
.collect(
Collectors.toCollection(PriorityQueue::new)
);
输出:[1015.45,1234.86,2348.26,8956.31,7351.03]
参考
Java -包Java .util.stream
Java - Java .util包