HashMap集合嵌套集合方法四种

Map<String, HashMap<Person, String>> m=new HashMap<String, HashMap<Person, String>>();
        HashMap<Person, String> h1=new HashMap<Person,String>();
        HashMap<Person, String> h2=new HashMap<Person,String>();
        
        h1.put(new Person("小张",16), "java1203");
        h1.put(new Person("小刘",17), "java1203");
        h2.put(new Person("小王",17), "java0999");
        h2.put(new Person("小溪",20), "java0999");
        
        m.put("甲骨文", h1);
        m.put("真古文", h2);

 

        //分别取出各项数据
        //1.keyset  and   for
        Set<String> s1=m.keySet();
        for(String s:s1)
        {
            //System.out.println(s);
            HashMap<Person, String> s2=m.get(s);
            Set<Person> s3=s2.keySet();
            for(Person p:s3)
            {
                System.out.println("学校名称为"+s+"班级名称为"+s2.get(p)+"姓名为"+p.getName()+"年龄"+p.getAge());
            }
        }

 

        //2.keyset and iter
        Set<String> s=m.keySet();
        
        for(Iterator<String> i=s.iterator();i.hasNext();)
        {
            String ss=i.next();
            //System.out.println(ss);
            HashMap<Person, String> schoolname=m.get(ss);
            Set<Person> s2=schoolname.keySet();
            Iterator<Person> i2=s2.iterator();
            while(i2.hasNext())
            {
                Person p=i2.next();
                //System.out.println(p);
                String classname=schoolname.get(p);
                System.out.println("学校名称为"+ss+"班级名称为"+classname+"姓名为"+p.getName()+"年龄"+p.getAge());
                
            }
        }

 

        //3.map.e  and    for
        Set<Map.Entry<String, HashMap<Person, String>>> s=m.entrySet();
        for(Map.Entry<String, HashMap<Person, String>> m2:s)
        {
            String schoolname=m2.getKey();
            HashMap<Person, String> h=m2.getValue();
            Set<Map.Entry<Person, String>> s3=h.entrySet();
            for(Map.Entry<Person, String> m3:s3)
            {
                Person p=m3.getKey();
                String classname=m3.getValue();
                System.out.println("学校名称为"+schoolname+"班级名称为"+classname+"姓名为"+p.getName()+"年龄"+p.getAge());
            }
        }

 

     

        //4.map.e  and   iter
        Set<Map.Entry<String, HashMap<Person, String>>> s=m.entrySet();
        Iterator<Map.Entry<String, HashMap<Person, String>>> i=s.iterator();
        while(i.hasNext())
        {
            Map.Entry<String, HashMap<Person, String>> a=i.next();
            String schoolname=a.getKey();
            HashMap<Person, String> h=a.getValue();
            Set<Map.Entry<Person, String>> s2=h.entrySet();
            
            for(Iterator<Map.Entry<Person, String>> i2=s2.iterator();i2.hasNext();)
            {
                Map.Entry<Person, String> mm=i2.next();
                Person p=mm.getKey();
                String classname=mm.getValue();
                System.out.println("学校名称为"+schoolname+"班级名称为"+classname+"姓名为"+p.getName()+"年龄"+p.getAge());

            }
            
        }
        
    }

 


        

        

posted @ 2019-03-02 22:59  一半人生  阅读(930)  评论(0编辑  收藏  举报