map接口-集合嵌套

public static void main(String[] args) {
        HashMap<String, HashMap<Person,String>> Bigmap=new 
                HashMap<String, HashMap<Person,String>>();
        //小map
        HashMap<Person,String> smap1=new HashMap<Person,String>();
        smap1.put(new Person("海绵宝宝",18),"java0429");
        smap1.put(new Person("派大星",30),"java0429");
        HashMap<Person,String> smap2=new HashMap<Person,String>();
        smap2.put(new Person("史迪仔",18),"java0226");
        smap2.put(new Person("小红帽",30),"java0226");
        //存入大map
        Bigmap.put("class1", smap1);
        Bigmap.put("class2", smap2);
        //1.keySet+增强for
        //获取所有key所在的set集合
        Set<String> bigKeys=Bigmap.keySet();
        //循环遍历
        //得到大map中每一个key值(class1,class2)
        for(String bigKey:bigKeys){
            //根据key得到每一个value值(smap1,smap2)
            HashMap<Person,String> bigValue=Bigmap.get(bigKey);
            //获取所有小map中key所在的set集合
            Set<Person> skeys=bigValue.keySet();
            //循环遍历取到每一个小key值(Person 对象)
            for(Person skey:skeys){
                //根据可以获取每一个value值
                String svalue=bigValue.get(skey);
                System.out.println(bigKey+".."+skey+".."+svalue);
            }
        }
    }

1.keySet+增强for

 

2.entrySet+Iterator迭代器

public static void main(String[] args) {
        HashMap<String, HashMap<Person,String>> Bigmap=new 
                HashMap<String, HashMap<Person,String>>();
        //小map
        HashMap<Person,String> smap1=new HashMap<Person,String>();
        smap1.put(new Person("海绵宝宝",18),"java0429");
        smap1.put(new Person("派大星",30),"java0429");
        HashMap<Person,String> smap2=new HashMap<Person,String>();
        smap2.put(new Person("史迪仔",18),"java0226");
        smap2.put(new Person("小红帽",30),"java0226");
        //存入大map
        Bigmap.put("class1", smap1);
        Bigmap.put("class2", smap2);
        //entrySet+Iterator迭代器
        //获取所有entry对象所在的set集合
        Set<Entry<String, HashMap<Person,String>>> bigEntrys=Bigmap.entrySet();
        //获取迭代器对象
        Iterator<Entry<String, HashMap<Person,String>>> it=bigEntrys.iterator();
        //循环遍历取到每一个entry对象(bigEntry)
        while(it.hasNext()){
            Entry<String, HashMap<Person,String>> bigEntry=it.next();
            //获取bigkey
            String bigkey=bigEntry.getKey();
            //获取bigvalue
            HashMap<Person,String> bigvalue=bigEntry.getValue();
            //获取小map中的所有entry对象所在的set集合
            Set<Entry<Person,String>> sentrys=bigvalue.entrySet();
            //获取迭代器对象
            Iterator<Entry<Person,String>> sit=sentrys.iterator();
            //循环遍历取到每一个entry对象
            while(sit.hasNext()){
                Entry<Person,String> sentry=sit.next();
                //获取skey
                Person skey=sentry.getKey();
                ///获取svalue
                String svalue=sentry.getValue();
                System.out.println(bigkey+".."+skey+".."+svalue);
            }
        }
        
    }

3.模拟斗地主洗牌发牌

public static void main(String[] args) {
        //封装牌
        Map<Integer,String> pooker=new HashMap<Integer,String>();
        ArrayList<Integer> pookerNumber=new ArrayList<Integer>();
        //封装牌号
        String[] number={"2","a","K","Q","J","10","9","8","7","6","5","4","3"};
        //封装花色
        String[] color={"?","?","?","?"};
        int index=2;
        for(String n:number){
            for(String c:color){
                pooker.put(index, c+n);
                pookerNumber.add(index);
                index++;
            }
        }
        //封装大小王
        pooker.put(0, "大王");
        pooker.put(1, "小王");
        pookerNumber.add(0);
        pookerNumber.add(1);
        //洗牌
        Collections.shuffle(pookerNumber);
        //发牌
        ArrayList<Integer> player1=new ArrayList<Integer>();
        ArrayList<Integer> player2=new ArrayList<Integer>();
        ArrayList<Integer> player3=new ArrayList<Integer>();
        ArrayList<Integer> bottom=new ArrayList<Integer>();
        for (int i = 0; i < pookerNumber.size(); i++) {
            //前三张给底牌
            if(i<3){
                bottom.add(pookerNumber.get(i));
            }else if(i%3==0){
                player1.add(pookerNumber.get(i));
            }else if(i%3==1){
                player2.add(pookerNumber.get(i));
            }else if(i%3==2){
                player3.add(pookerNumber.get(i));
            }
        }
        //排序
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);
        Collections.sort(bottom);
        //看牌
        look("渣渣辉",player1,pooker);
        look("刘嘉玲",player2,pooker);
        look("古天乐",player3,pooker);
        look("底牌",bottom,pooker);
    }
    public static void look(String name,ArrayList<Integer> player,Map<Integer,String> pooker){
        System.out.print(name+":");
        for (int key:player) {
            System.out.print(pooker.get(key)+" ");
        }
        System.out.println();
    }

 

map接口-集合嵌套

上一篇:Mybatis基础知识


下一篇:常用公共配置类——定时任务配置