java - 使用 Java 8 Streams 对 Map 的值(Set -> SortedSet)进行排序
问题描述
如何使用流对Map<String, Set<String>>
ie 的值进行排序转换为Map<String, SortedSet<String>
>?
解决方案
只需遍历每个条目并将 (eg ) 转换为Set<T>
( HashSet<T>
eg SortedSet<T>
)TreeSet<T>
为:
Map<String, Set<String>> input = new HashMap<>();
Map<String, SortedSet<String>> output = new HashMap<>();
input.forEach((k, v) -> output.put(k, new TreeSet<>(v)));
或流为:
Map<String, Set<String>> input = new HashMap<>();
Map<String, SortedSet<String>> output = input.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, a -> new TreeSet<>(a.getValue())));
推荐阅读
- docker-compose - Docker-compose:与 selinux 一起使用
- javascript - 如何在 chart.js 中制作曲线条形图或曲线柱形图?
- angular - Firebase 通知适用于后台和前台
- android - 全屏活动中的状态栏高度
- android - 如何在android studio中创建这样的微调器?
- swift - 选择新按钮后 UICollectionView 不释放内存
- python - 如何将最低键和最低值配对在一起(它应该按从小到大的顺序上升)?
- neo4j - Neo4j 查询执行时间:多次执行同一个查询时,似乎只有第一个是正确的
- tensorflow - 密集层对不同识别任务的一般用途
- python - 在 Python 3.7 中选择性地模拟模块