本文介绍了遍历带有arraylist的hashmap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个带有数组列表的基本哈希图:
So I have a basic hashmap with an arraylist:
Map<Text, ArrayList<Text>> map = new HashMap<Text, ArrayList<Text>>();
说我有一个键值对:键:Apple,值:橙色,红色,蓝色
Say I have a key value pair: Key: Apple, Value: orange, red, blue
我已经了解了如何遍历以打印密钥,其值如下所示:苹果,橙,红,蓝
I already understand how to iterate through to print the key and it’s values like so:Apple, orange, red, blue
但是有一种方法可以拆分值/通过内部ArrayList进行迭代,并分别打印键/值对3次/用每个值分别打印键,如:
but is there a way to break up the values/iterate through the inner ArrayList and print the key/value pair three separate times/print the key with each value separately like:
Apple orange
Apple red
Apple blue
推荐答案
使用简单的for
循环,将是:
Using simple for
loops, this would be:
for (Map.Entry<Text, ArrayList<Text>> entry : map.entrySet()) {
for (Text text : entry.value()) {
System.out.println(entry.key() + " " + text);
}
}
在功能上进行相同的操作:
Doing the same in a functional way:
map.forEach((key, valueList) ->
valueList.forEach(listItem -> System.out.println(key + " " + listItem)
));
这篇关于遍历带有arraylist的hashmap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!