List<Map<String, String>>和Map<String, List<String>>遍历
程序员文章站
2024-02-17 15:22:04
...
List<Map<String, String>>和Map<String, List>遍历
public void TestM() {
List<Map<String, String>> lm = new ArrayList<>();
Map<String, String> m = new HashMap<>();
m.put("1", "chenhe");
m.put("2", "wangchaonan");
Map<String, String> m1 = new HashMap<>();
m1.put("1", "xiaochenhe");
m1.put("2", "xiaowangchaonan");
lm.add(m);
lm.add(m1);
for (Map<String, String> map : lm) {
System.out.println(map + "============");
for (String mp : map.keySet()) {
System.out.println(mp + "_______________" + map.get(mp));
}
}
Map<String, List<String>> ml = new HashMap<>();
List<String> l = new ArrayList();
List<String> l1 = new ArrayList();
List<String> l2 = new ArrayList();
l.add("1");
l.add("2");
l.add("3");
l1.add("4");
l1.add("5");
l1.add("6");
l2.add("7");
l2.add("8");
l2.add("9");
ml.put("1", l);
ml.put("2", l1);
ml.put("3", l2);
for (String mm : ml.keySet()) {
System.out.println(mm + "------" + ml.get(mm));
Iterator<String> it = ml.get(mm).iterator();
if (it.hasNext()) {
System.out.println(ml.get(mm));
}
for (String mmm : ml.get(mm)) {
System.out.println(mmm);
}
}
}
下一篇: Map排序