Map类的三种实现 <个人练习>

 1 package cn.zmh.zuoye;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.Map;
 6 import java.util.Set;
 7 /*
 8 * 定义
 9 *   aaa学校
10 *     定义两个班级
11 *        java班  学号,姓名
12 *            001  张三1
13 *            002  张三2
14 *        hdoop班 学号,姓名
15 *            001  张三3
16 *            002  张三4
17 * */
18 public class MapDemo1 {
19     public static void main(String[] args) {
20         Map<String,String> javas = new HashMap<>();
21         Map<String,String> hdoop = new HashMap<>();
22         // 键值不能重复
23         javas.put("001","张三1");
24         javas.put("002","张三2");
25         hdoop.put("001","张三3");
26         hdoop.put("002","张三4");
27         Map<String,Map<String,String>> aaa = new HashMap<>();
28         aaa.put("java班",javas);
29         aaa.put("hdoop班",hdoop);
30         //调用方法 传参
31         fun1(aaa);
32     }
33     //第一种  迭代器 Iterator        entrySet();
34     public  static void fun1(Map<String, Map<String, String>> aaa) {
35         Set<Map.Entry<String,Map<String,String>>> classNameSet = aaa.entrySet();
36         Iterator<Map.Entry<String,Map<String,String>>> it = classNameSet.iterator();
37         while(it.hasNext()){
38             Map.Entry<String, Map<String, String>> next = it.next();
39             String classNamekey = next.getKey();
40             Map<String, String> classNameValue = next.getValue();
41             System.out.println(classNamekey);
42             Set<Map.Entry<String, String>> studentSet = classNameValue.entrySet();
43             Iterator<Map.Entry<String,String>> studendIt = studentSet.iterator();
44             while (studendIt.hasNext()){
45                 Map.Entry<String, String> next1 = studendIt.next();
46                 String studentkey = next1.getKey();
47                 String studendValue = next1.getValue();
48                 System.out.println("\t"+studentkey+":"+studendValue);
49             }
50         }
51     }
52     //第二种  增强for循环    entrySet();
53     public static void fun2(Map<String, Map<String, String>> aaa) {
54         Set<Map.Entry<String, Map<String, String>>> classNameSet = aaa.entrySet();
55         for(Map.Entry<String,Map<String,String>> i:classNameSet){
56             String classNamekey = i.getKey();
57             Map<String, String> classNameValue = i.getValue();
58             System.out.println(classNamekey);
59             Set<Map.Entry<String, String>> studentSet = classNameValue.entrySet();
60             for(Map.Entry<String, String> i1:studentSet){
61                 String studentkey = i1.getKey();
62                 String studentValue = i1.getValue();
63                 System.out.println("\t"+studentkey+":"+studentValue);
64             }
65         }
66     }
67     //第三种 迭代器 Iterator     方法keySet();
68     public static void fun3(Map<String, Map<String, String>> aaa) {
69         Set<String> classNameSet = aaa.keySet();
70         Iterator<String> it = classNameSet.iterator();
71         while (it.hasNext()){
72             String classNameKey = it.next();
73             Map<String, String> classNameValue = aaa.get(classNameKey);
74             System.out.println(classNameKey);
75             //System.out.println(classNameValue);
76             Set<String> studentSet = classNameValue.keySet();
77             Iterator<String> it1 = studentSet.iterator();
78             while (it1.hasNext()){
79                 String studentKey = it1.next();
80                 //System.out.println(studentKey);
81                 String studentValue = classNameValue.get(studentKey);
82                 System.out.println(studentKey+":"+studentValue);
83             }
84         }
85     }
86 }

 打印结果

posted @ 2019-03-24 13:26  Java练习生  阅读(708)  评论(0编辑  收藏  举报