zoukankan      html  css  js  c++  java
  • Map的遍历

    HashMap<String,Hashmap<Person,String>> MAP=new HashMap<String,HashMap<Person,String>();

    HashMap<Person,String> map=new HashMap<Person,String>();

    有四种遍历:

    keySet()+增强for:获取key,然后通过增强for,遍历key,通过get(key)获取value值

    keySet()+Iterator:通过key的迭代器Iterator对象,while迭代,.next()遍历key,get(key)获取value值

    entrySet()+增强for :entrySet()结婚证对象,再通过增强for遍历该对象,通过getKey() getValue()取值

    entrySet()+Iterator:创建entrySet()结婚证对象的迭代器Iterator对象,while迭代,通过.next()的遍历该对象,getKey() getValue()取值

    public class Demo04 {
        public static void main(String[] args) {
            HashMap<Person, String> java0723=new HashMap<Person,String>();
            HashMap<Person,String> java0611=new HashMap<Person,String>();
            HashMap<String, HashMap<Person, String>> oracle=new HashMap<String,HashMap<Person,String>>(); 
            //内部赋值
            java0723.put(new Person("武千策",22), "男");
            java0723.put(new Person("许勇革",21), "男");
            java0611.put(new Person("王伟业",20), "男");    
            java0611.put(new Person("宋义",25), "男");        
            //外部赋值
            oracle.put("java0611", java0611);
            oracle.put("java0723", java0723);
            get1(oracle);
            get2(oracle);
            get3(oracle);
            get4(oracle);
            get5(oracle);
            get6(oracle);
            get7(oracle);
            }
            //entrySet+interator外     
            public static void get7(HashMap<String,HashMap<Person,String>> oracle){
                System.out.println("get7方法");
                Set<Map.Entry<String,HashMap<Person,String>>> set=oracle.entrySet();
                Iterator<Map.Entry<String,HashMap<Person,String>>> it=set.iterator();
                while(it.hasNext()){
                    Map.Entry<String,HashMap<Person,String>> out=it.next();
                    //System.out.println("外key"+out.getKey()+"外value"+out.getValue());
                    Set<Map.Entry<Person,String>> setin=out.getValue().entrySet();
                    Iterator<Map.Entry<Person,String>> itin=setin.iterator();
                    while(itin.hasNext()){
                        //遍历内部对象
                        Map.Entry<Person,String> in=itin.next();
                        System.out.println("外部key为:"+out.getKey()+",内部key为:"+in.getKey()
                        +",内部value为:"+in.getValue());
                    }    
                }    
            }
            //keySet+iterator外   keySet+Iterator内
            public static void get6(HashMap<String,HashMap<Person,String>> oracle){
                System.out.println("get6方法");
                Set<String> set=oracle.keySet();
                Iterator<String> it=set.iterator();
                while(it.hasNext()){
                    String outkey=it.next();
                    //System.out.println(outkey+oracle.get(outkey));
                    //获取内部的key
                    Set<Person> inkey=oracle.get(outkey).keySet();
                    //内部Key的迭代器对象
                    Iterator<Person> itin=oracle.get(outkey).keySet().iterator();
                    //对内部key遍历
                    while(itin.hasNext()){
                        //内部key值
                        Person p=itin.next();
                        System.out.println("外部key为:"+outkey+",内部key为:"+p+",内部value为:"+oracle.get(outkey).get(p));
                    }
                }
            }
            //keySet+iterator外  +keySet+增强for
            public static void get5(HashMap<String,HashMap<Person,String>> oracle){
                System.out.println("get5方法");
                Set<String> set=oracle.keySet();
                Iterator<String> it=set.iterator();
                while(it.hasNext()){
                    //外部key
                    String outkey=it.next();
                    //System.out.println("外部key"+outkey+"外部value"+oracle.get(outkey));
                    //获取内部key的对象
                    Set<Person> inkey=oracle.get(outkey).keySet();
                    //遍历内部Key
                    for(Person init:inkey){
                        System.out.println("外部key为:"+outkey+",内部key为:"+init
                        +",内部value为:"+oracle.get(outkey).get(init));
                    }
                }
            }
            //entrySet+Iterator外  entrySet+增强for内
            public static void get4(HashMap<String,HashMap<Person,String>> oracle){
                System.out.println("get4方法");
                //外部对象的结婚证对象
                Set<Map.Entry<String,HashMap<Person,String>>> set=oracle.entrySet();
                //获取迭代器对象
                Iterator<Map.Entry<String,HashMap<Person,String>>> it=set.iterator();
                while(it.hasNext()){
                    Map.Entry<String,HashMap<Person,String>> out=it.next();
                    //System.out.println("外部key"+out.getKey()+"外部value"+out.getValue());
                    //获取内部对象的结婚证对象
                    Set<Map.Entry<Person,String>> in=out.getValue().entrySet();
                    for(Map.Entry<Person,String> setin:in){
                        System.out.println("外部key为:"+out.getKey()+",内部key为:"+setin.getKey()
                        +",内部value为:"+setin.getValue());
                    }
                }    
            }
            //entrySet+Iterator外 entrySet+Iterator内
            public static void get3(HashMap<String,HashMap<Person,String>> oracle){
                System.out.println("get3方法");
                //获取外部结婚证对象
                Set<Map.Entry<String,HashMap<Person,String>>> set=oracle.entrySet();
                //获取外部对象的迭代器对象
                Iterator<Map.Entry<String,HashMap<Person,String>>> it=set.iterator();
                //迭代遍历
                while(it.hasNext()){
                    //获取外部对象out
                    Map.Entry<String,HashMap<Person,String>> out=it.next();
                    //System.out.println("外部对象的key为"+out.getKey()+",外部对象的value为"+out.getValue());
                    //获取内部对象的 结婚证对象的 迭代器对象itin,内部对象=外部对象的value=out.getValue();
                    Iterator<Map.Entry<Person, String>> itin=out.getValue().entrySet().iterator();
                    while(itin.hasNext()){
                        //获取内部对象in
                        Map.Entry<Person,String> in=itin.next();
                        System.out.println("外部key为:"+out.getKey()+",内部key为:"+in.getKey()+",内部value为:"+in.getValue());
                    }
                }
                        
            }
            
            //keySet+增强for外  keySet+增强for内
            public static void get1(HashMap<String,HashMap<Person,String>> oracle){
                System.out.println("get1方法");
                //1.获取装有外部所有key的set集合
                Set<String> bigKeys=oracle.keySet();
                //2.循环遍历外部的每一个Key
                for(String bigkey:bigKeys){
                    //根据每一个Key获取对应的value
                    HashMap<Person,String> bigValue=oracle.get(bigkey);
                    //获取内部的所有key所在的集合
                    Set<Person> keys=bigValue.keySet();
                    //循环遍历获取到每一个内部key
                    for(Person key:keys){
                        System.out.println(bigkey+":"+key+":"+bigValue.get(key));
                    }
                }
            }
            //entrySet+Iterator外 entrySet+Iterator内
            public static void get2(HashMap<String,HashMap<Person,String>> oracle){
                System.out.println("get2方法");
                //获取外部结婚证对象集合
                Set<Map.Entry<String,HashMap<Person,String>>> bigentrys=oracle.entrySet();
                //获取迭代器对象
                Iterator<Map.Entry<String,HashMap<Person,String>>> bigit=bigentrys.iterator();
                //循环遍历外部
                while(bigit.hasNext()){
                    //获取每一个结婚证对象
                    Map.Entry<String,HashMap<Person,String>> bigentry=bigit.next();
                    //从每一个结婚证对象中获取外部的key
                    String bigkey=bigentry.getKey();
                    //获取外部的value
                    HashMap<Person,String> bigvalue=bigentry.getValue();
                    //获取内部的所有结婚证对象集合
                    Set<Map.Entry<Person,String>> sentry=bigvalue.entrySet();
                    //获取内部所有结婚证对象的迭代器对象
                    Iterator<Map.Entry<Person,String>> sit=sentry.iterator();
                    while(sit.hasNext()){
                        //获取每一个内部的结婚证对象
                        Map.Entry<Person,String> entry=sit.next();
                        //获取每一个内部的key
                        Person skey=entry.getKey();
                        String svalue=entry.getValue();
                        System.out.println(bigkey+":"+skey+":"+svalue);
                    }        
                }
            }
            
        
    }
  • 相关阅读:
    nginx rewrite 模块
    nginx 的请求处理流程
    nginx keepalive 长连接
    cglib、orika、spring等bean copy工具性能测试和原理分析
    jackson、fastjson、kryo、protostuff等序列化工具性能对比
    Java源码详解系列(十一)--Spring的使用和源码(共计4篇博客)
    Spring源码系列(四)--spring-aop是如何设计的
    Spring源码系列(三)--spring-aop的基础组件、架构和使用
    JMH--一款由OpenJDK开发的基准测试工具
    MySQL系列文章汇总
  • 原文地址:https://www.cnblogs.com/god3064371/p/11608642.html
Copyright © 2011-2022 走看看