Map嵌套Map:
例:
AAA:
Javas班:
001 熊大
002 熊二
Hdoop班
001 小猪猪
002 小菲菲
★使用增强for循环遍历Set数组:
import java.util.HashMap; import java.util.Set; import java.util.Map.Entry; public class MapDemo { public static void main(String[] args) { //定义javas班的集合 HashMap<String, String> javas=new HashMap<String,String>(); //定义Hdoop班集合 HashMap<String, String> Hdoop=new HashMap<String,String>(); //向班级存储学生 javas.put("001", "熊大"); javas.put("002", "熊二"); Hdoop.put("001", "小猪猪"); Hdoop.put("002", "小菲菲"); //定义AAA容器,键是班级的名字,值是两个班级的容器 HashMap<String, HashMap<String, String>> AAA=new HashMap<String, HashMap<String, String>>(); AAA.put("javas班", javas); AAA.put("Hdoop班", Hdoop); entrySet1(AAA); } private static void entrySet1(HashMap<String, HashMap<String, String>> AAA) { //调用集合AAA的方法,entrySet将AAA集合的键封装到Set集合中。 Set<Entry<String, HashMap<String, String>>> aaa=AAA.entrySet(); //增强for循环遍历set集合 for(Entry<String, HashMap<String, String>> p:aaa){ //System.out.println(p); //getkey获得AAA的键,getValue获得值 String classNameKey=p.getKey(); HashMap<String, String> classMap =p.getValue(); System.out.println(classNameKey); //System.out.println(classMap); //将classMap装进Set集合 Set<Entry<String, String>> s=classMap.entrySet(); for (Entry<String, String> q:s) { //getKey获得班级的键,getValue获得值 String numKey=q.getKey(); String nameValue=q.getValue(); System.out.println(numKey+": "+nameValue); } } } }
★ 使用迭代器遍历Set数组
import java.util.Iterator; import java.util.Map.Entry; import java.util.HashMap; import java.util.Set; //Map嵌套存储Map public class MaoMapDemo { public static void main(String[] args) { //定义javas班的集合 HashMap<String, String> javas=new HashMap<String,String>(); //定义Hdoop班集合 HashMap<String, String> Hdoop=new HashMap<String,String>(); //向班级存储学生 javas.put("001", "熊大"); javas.put("002", "熊二"); Hdoop.put("001", "小猪猪"); Hdoop.put("002", "小菲菲"); //定义AAA容器,键是班级的名字,值是两个班级的容器 HashMap<String, HashMap<String, String>> AAA=new HashMap<String, HashMap<String, String>>(); AAA.put("javas班", javas); AAA.put("Hdoop班", Hdoop); entrySet1(AAA); } private static void entrySet1(HashMap<String, HashMap<String, String>> AAA) { //调用集合AAA的方法,entrySet将AAA集合的键封装到Set集合中。 Set<Entry<String, HashMap<String, String>>> classNameSet=AAA.entrySet(); /*迭代Set集合*/ //集合绑定迭代器 Iterator<Entry<String, HashMap<String, String>>> it=classNameSet.iterator(); while (it.hasNext()) { //遍历集合 Entry<String, HashMap<String, String>> next= it.next(); //System.out.println(next); //getkey获得键,getValue获得值 String classNameKey=next.getKey(); HashMap<String, String> classMap = next.getValue(); //AAA容器的键,班级名字classNameKey System.out.println(classNameKey); //AAA容器的值,班级所有元素classMap //System.out.println(classMap); //entrySet将classMap集合的键封装到Set集合中。 Set<Entry<String, String>> studentSet=classMap.entrySet(); //迭代,集合绑定迭代器 Iterator<Entry<String, String>> studentIt=studentSet.iterator(); while (studentIt.hasNext()) { //遍历集合 Entry<String, String> studentEntry = studentIt.next(); //getkey获得键,getValue获得值 String numKey=studentEntry.getKey(); String nameValue=studentEntry.getValue(); System.out.println(numKey+": "+nameValue); } } } }