有什么方法可以提取Map的值? [英] Is there any way to extract Map's values?
本文介绍了有什么方法可以提取Map的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试从以下数据类型中提取值. Map<String, Map<String, Integer>>,
样本数据是这样的.
I try to extract values from the below data type. Map<String, Map<String, Integer>>,
The sample data is like this.
- ("aaa",地图("bbb",333))
- ("ddd",地图("ccc",444))
- ("aaa", Map("bbb",333))
- ("ddd", Map("ccc",444))
我想要的结果
HashMap
key : bbb, value : 333
key : ccc, value : 444
我尝试过
mapData.values().stream()
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue))
但是失败了.有什么好的方法可以将嵌套的Map
中的值部分提取为Map?
but got failed. Is there any good way to extract the values part as Map in the nested Map
?
推荐答案
您需要flatMap
内部地图的输入-
You need to flatMap
your entries of the inner map -
Map<String, Integer> output = map.values().stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
这篇关于有什么方法可以提取Map的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文