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);
                
            }
            
            }
                
                
            }
            

    }

转载于:https://www.cnblogs.com/hph1728390/p/10580751.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值