zoukankan      html  css  js  c++  java
  • Map 嵌套存储Map

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.Map.Entry;

    public class MapDome {

         //  Map 嵌套存储Map
        //      aaa
        //        java班
        //          001 郭嘉
        //          002 神郭嘉
        //        javahoodp班
        //          001 黄月英
        //          002 神黄月英
        


        //      java班 :存学号和名字
        //      javahoodp班:存学号和名字
        //      学校:存的是班级
        //          java班<学号,姓名>
        //          aaaMap<班级名字,java班>
       
        public static void main(String[] args) {

    //定义Java班的集合
            HashMap<String, String> java=new HashMap<String, String>();

        //定义java班级的学生


                java.put("001","郭嘉");
                java.put("002", "神郭嘉");

        //定义hdoop班的集合
                
            HashMap<String, String> hdoop=new HashMap<String,String>();

        //向hdoop班保存学生


                hdoop.put("001","黄月英");
                hdoop.put("002", "神黄月英");


              //定义学校的集合


            HashMap<String, HashMap<String, String>> a=new HashMap<String,HashMap<String, String>>();

        //定义学校是容器    键是班级的名字  值是两个


                a.put("java班", java);
                a.put("hdoop班", hdoop);
            

          //调用集合aaa  的方法    entrySet 将学校集合的键封装到Set集合中
                Set<Entry<String, HashMap<String, String>>> Set=a.entrySet();

        //增强for循环遍历集合

        //获取的是学校的集合


                for(Entry<String, HashMap<String, String>> s:Set){

        //获取getkey 和getvalue的值


                    String key = s.getKey();
                    HashMap<String, String> value = s.getValue();
                    
                    System.out.println(key);
                    
                    现在获取的是两个班级的集合


                    Set<Entry<String, String>> enSet = value.entrySet();

          //使用增强for循环,循环Set集合


                    for(Entry<String, String>l:enSet){
                        String key2 = l.getKey();
                        String value2 = l.getValue();
                        System.out.println(key2+"         "+value2);
                    }
                }


                System.out.println("+++++++++++++++++++++++++++++++++++");

        //使用迭代  set集合
                Iterator<Entry<String, HashMap<String, String>>> it = Set.iterator();


                while (it.hasNext()) {


                    Entry<String, HashMap<String, String>> next = it.next();
                    String key = next.getKey();
                    HashMap<String, String> value = next.getValue();
                    
                Set<Entry<String, String>> enSet = value.entrySet();
                
                Iterator<Entry<String, String>> ite = enSet.iterator();


                while (ite.hasNext()) {


                    Entry<String, String> next2 = ite.next();
                    String key2 = next2.getKey();
                    String value2 = next2.getValue();
                    System.out.println(key2+"         "+value2);
                    
                }
                
                }
                    
                    
                }
                

        }

  • 相关阅读:
    什么是序列化
    命令执行漏洞
    sql注入总结
    npm包之merge-descriptors
    Koa路由中间件之koa-router
    TypeScript声明文件(.d.ts)的使用
    TypeScript使用的简单记录
    TypeScript的安装、使用及配置
    Node websocket简单封装
    使用docker-compose配置mysql服务
  • 原文地址:https://www.cnblogs.com/hph1728390/p/10580751.html
Copyright © 2011-2022 走看看