[英]Printing a Map with a nested Map
I have a Map of the following kind: HashMap<String, Map<String, Integer>> resultMap = new HashMap<>();
我有以下类型的 Map:
HashMap<String, Map<String, Integer>> resultMap = new HashMap<>();
Where String(Key) = Website address;
其中
String(Key) = Website address;
Map<String,Integer> = String(key) -search word, Integer(value) - counter for found words.
How to print the Map correctly so that it looks like this:如何正确打印 Map 使其看起来像这样:
Thanks in advance for any ideas!提前感谢您的任何想法!
Map
has simple iterator forEach((key, value) -> your_consumer)
, and the entries in the nested map may be converted into strings joined using Collectors.joining
, so printing may be done as follows: Map
具有简单的迭代器forEach((key, value) -> your_consumer)
,嵌套 map 中的条目可以转换为使用Collectors.joining
连接的字符串,因此可以按如下方式打印:
resultMap.forEach((k, v) ->
System.out.println(k + " - " +
v.entrySet().stream()
.map(e -> e.getKey() + "=" + e.getValue())
.collect(Collectors.joining(", "))
);
If I've correctly understood you: In the outer loop, you should iterate the nested maps (as values), and in the internal loop(s) you can finally iterate keys and values of the nested map.如果我对您的理解正确:在外部循环中,您应该迭代嵌套映射(作为值),而在内部循环中,您最终可以迭代嵌套 map 的键和值。
HashMap<String, Map<String, Integer>> map = new HashMap<>();
for (Map<String, Integer> nestedMap : map.values())
{
for (String key : nestedMap.keySet()) {
// some actions here
}
for (Integer value : nestedMap.values()) {
// some actions here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.