package string.test; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /* * (Map扩展)方式1 */ public class MapDemo7 { public static void main(String[] args) { Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>(); Map<String, String> banjia = new HashMap<String, String>(); banjia.put("a01", "zhangsan"); banjia.put("a02", "lisi"); Map<String, String> banjib = new HashMap<String, String>(); banjib.put("b01", "wangwu"); banjib.put("b02", "zhaoliu"); map.put("banjia", banjia); map.put("banjib", banjib); Set<Map.Entry<String, Map<String, String>>> entrySet = map.entrySet(); Iterator<Map.Entry<String, Map<String, String>>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, Map<String, String>> me = iterator.next(); String key = me.getKey(); Map<String, String> value = me.getValue(); System.out.println(key); writeMap(value); } } public static void writeMap(Map<String, String> map) { Set<Map.Entry<String, String>> entrySet = map.entrySet(); Iterator<Map.Entry<String, String>> it = entrySet.iterator(); while (it.hasNext()) { Map.Entry<String, String> me = it.next(); System.out.println("key:" + me.getKey() + ",value:" + me.getValue()); } } }
package string.test; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; /* * (Map扩展)方式2 */ public class MapDemo8 { public static void main(String[] args) { Map<String, List<StudentDemo>> map = new HashMap<String, List<StudentDemo>>(); List<StudentDemo> banjia = new ArrayList<StudentDemo>(); banjia.add(new StudentDemo("a01", "zhangsan")); banjia.add(new StudentDemo("a02", "lisi")); List<StudentDemo> banjib = new ArrayList<StudentDemo>(); banjib.add(new StudentDemo("b01", "wangwu")); banjib.add(new StudentDemo("b02", "zhaoliu")); map.put("banjia", banjia); map.put("banjib", banjib); Set<Map.Entry<String, List<StudentDemo>>> entrySet = map.entrySet(); Iterator<Map.Entry<String, List<StudentDemo>>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, List<StudentDemo>> me = iterator.next(); String key = me.getKey(); List<StudentDemo> value = me.getValue(); System.out.println(key); writeMap(value); } } public static void writeMap(List<StudentDemo> stus) { Iterator<StudentDemo> it = stus.iterator(); while (it.hasNext()) { StudentDemo me = it.next(); System.out.println("key:" + me.getId() + ",value:" + me.getName()); } } } class StudentDemo { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public StudentDemo(String id, String name) { this.id = id; this.name = name; } }