zoukankan      html  css  js  c++  java
  • Map的嵌套使用

    Map的嵌套使用

    Map嵌套Map:

    例:

      AAAA:

        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);
                }
            }
            
        }
    }
    复制代码
  • 相关阅读:
    算法两则
    windows XP 神key
    mysql空间型数据使用python executemany批量插入报错
    关于集合的相似度测量方法
    读取经纬度坐标并存储为字典格式,即key为ID,value为轨迹点
    ubuntu下安装软件时报错解决:Unmet dependencies. Try 'apt-get -f install' with no packages
    ubuntu环境下pycharm编译程序import包出错:ImportError: dynamic module does not define init function (init_caffe)
    linux Ubuntu14.04 make编译文件报错:No rule to make target `/usr/lib/libpython2.7.so', needed by `python/_pywraps2.so'. Stop.
    U盘安装Ubuntu14.04&配置远程win10远程连接
    解决:error LNK1169: 找到一个或多个多重定义的符号
  • 原文地址:https://www.cnblogs.com/cuichaobo/p/10681301.html
Copyright © 2011-2022 走看看