所以我有一个带有数组列表的基本哈希图:Map<Text, ArrayList<Text>> map = new HashMap<Text, ArrayList<Text>>();假设我有一个键值对:键:苹果,值:橙色、红色、蓝色我已经了解如何迭代以打印密钥及其值,如下所示:Apple、orange、red、blue但是有没有办法通过内部 ArrayList 分解值/迭代并将键/值对打印三次/分别打印每个值的键,例如:Apple orangeApple redApple blue
2 回答
婷婷同学_
TA贡献1844条经验 获得超8个赞
您可以使用嵌套循环:
for (Map.Entry<Text, ArrayList<Text>> entry : map.entrySet()) {
for (Text text : entry.value()) {
System.out.println(entry.key() + " " + text);
}
}
牛魔王的故事
TA贡献1830条经验 获得超3个赞
使用简单的for循环,这将是:
for (Map.Entry<Text, ArrayList<Text>> entry : map.entrySet()) {
for (Text text : entry.value()) {
System.out.println(entry.key() + " " + text);
}
}
以功能方式做同样的事情:
map.forEach((key, valueList) ->
valueList.forEach(listItem -> System.out.println(key + " " + listItem)
));
添加回答
举报
0/150
提交
取消