我有這張地圖:
Map<String, List<Map<String, Integer>>> map = new HashMap<>();
我想通過使用經典的 for 回圈而不使用其他方法(例如 Iterator)來迭代此 Map
謝謝!
uj5u.com熱心網友回復:
是的,我們可以做到。在下面撰寫了示例實作。希望它應該有所幫助。
public class TestDummy {
public static void main(String[] args) {
Map<String, Integer> temp = new HashMap<>();
temp.put("harish", 1);
List<Map<String, Integer>> tempList = new ArrayList<>();
tempList.add(temp);
Map<String, List<Map<String, Integer>>> map = new HashMap<>();
map.put("shyam", tempList);
for (Map.Entry<String, List<Map<String, Integer>>> entry : map.entrySet()) {
System.out.println("[Key] : " entry.getKey() " [Value] : " entry.getValue());
}
}
}
輸出將如下:
[Key] : shyam [Value] : [{harish=1}]
uj5u.com熱心網友回復:
public static void main(String[] args) {
Map<String, List<Map<String, Integer>>> map = new HashMap<>();
map.put("Test", List.of(Map.of("TestValue", 1)));
map.put("Test2", List.of(Map.of("TestValue2", 2)));
Object[] entries = map.entrySet().toArray();
for (int i = 0; i < entries.length; i ) {
System.out.println(entries[i]);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416894.html
標籤:
下一篇:Python:垂直列印
