java - 合并流
问题描述
我正在尝试消除下面的临时 Map 并将其合并为一个Stream
。我在这里和那里尝试了一点,但没有找到解决方案。所以到目前为止我还没有可以展示的代码,因为我的方法可能会产生误导。
final Map<String, String> tempCountryMap = iso3166Alpha2CountryCodes.stream() //
.collect(Collectors.toMap(cc -> cc, cc -> new Locale("", cc).getDisplayCountry(locale)));
final Map<String, String> sortedMap = tempCountryMap.entrySet().stream() //
.sorted(Map.Entry.comparingByValue(Collator.getInstance(locale))) //
.collect(Collectors.toMap(Entry<String, String>::getKey, Entry<String, String>::getValue, (e1, e2) -> e2,
LinkedHashMap::new));
解决方案
如果将原始元素映射Stream
到Map.Entry
s 然后继续排序,则可以在单个管道中执行此操作:
final Map<String, String> sortedMap =
iso3166Alpha2CountryCodes.stream()
.map(cc -> new SimpleEntry<>(cc,new Locale("", cc).getDisplayCountry(locale)))
.sorted(Map.Entry.comparingByValue(Collator.getInstance(locale)))
.collect(Collectors.toMap(Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e2,
LinkedHashMap::new));
推荐阅读
- c++ - tbb::parallel_for 是否有中断命令
- firebase - 按特定文档查询集合
- xml - XSLT:如何匹配(在模板中)两个不同的元素
- node.js - 类不继承属性初始化状态
- php - 如何设置 Storage::disk('s3') 配置不在环境中,而是在文件中?
- sockets - 如何通过客户端的socketDataArrived方法正确接收数据
- c - 迭代存储在堆上的数组
- c++ - 如何在 C++ 中对包含自定义对象的容器使用分区函数
- swift - Xcode 编译错误“错误中止陷阱 6”
- typescript - Angular 7 每隔一段时间从 API 轮询/获取响应代码