Map的嵌套

package cn.lijun.demo2;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class MapMapDemo {
/*Map 嵌套存储Map
 * aaa  
 *    java97 班
 *       001   小明
 *       002   小红
 *    javaHoodp 班
 *       001   张三
 *       002   李四
 *       
 *    java班 :存学号和名字
 *    javahoodp:存学号和名字
 *    aaa 存班级
 * 
 */
//方法一 :keySet方法  迭代
    public static void main(String[] args) {
       //存放java97班
        HashMap<String,String> java=new HashMap<String,String>();
        java.put("001", "小明");
        java.put("002","小红");
    //存放hashhoodp
        HashMap<String,String> javahoodp=new HashMap<String,String>();
        javahoodp.put("001", "张三");
        javahoodp.put("002", "小红");
        //存放aaa
        HashMap<String,HashMap<String,String>> aaa=new HashMap<String,HashMap<String,String>>();
        aaa.put("java班", java);
        aaa.put("javahoodp", javahoodp);
        //用keySet方法把键值对放在set集合
        Set<String> k = aaa.keySet();
        Iterator<String> it = k.iterator();
        while(it.hasNext()){
            String akey = it.next();
            System.out.println(akey);
            HashMap<String, String> aValue = aaa.get(akey);
            System.out.println(aValue);
                
        Set<String> skey = aValue.keySet();
        System.out.println(skey);
        Iterator<String> sit = skey.iterator();
        while(sit.hasNext()){
            String studentkey = sit.next();
            String svalue = aValue.get(studentkey);
            System.out.println(studentkey+"  "+svalue);
        }
        }
        
        
        
    }

}

//方法二  利用entrySet
public class MapMapDemo3 {
    public static void main(String[] args) {
        HashMap<String,String> java=new HashMap<String,String>();
        java.put("001", "小明");
        java.put("002", "小红");
        
        HashMap<String,String> javahoodp=new HashMap<String,String>();
        javahoodp.put("001","张三");
        javahoodp.put("002", "李四");
        
        HashMap<String,HashMap<String,String>> aaa=new HashMap<String,HashMap<String,String>>();
        aaa.put("java班",java);
        aaa.put("javahoodp", javahoodp);
        entrySet1(aaa);
        
        
    }

    private static void entrySet1(HashMap<String, HashMap<String, String>> aaa) {
        Set<Entry<String, HashMap<String, String>>> entrySet = aaa.entrySet();
        Iterator<Entry<String, HashMap<String, String>>> it = entrySet.iterator();
        while(it.hasNext()){
            Entry<String, HashMap<String, String>> next = it.next();
            String akey = next.getKey();
            HashMap<String, String> avalue = next.getValue();
            System.out.println(akey);
            System.out.println(avalue);
            
            Set<Entry<String, String>> student = avalue.entrySet();
            Iterator<Entry<String, String>> sit = student.iterator();
            while(sit.hasNext()){
                Entry<String, String> next2 = sit.next();
                String skey = next2.getKey();
                String svalue = next2.getValue();
                System.out.println(skey+"  "+svalue);
                //System.out.println(svalue);
            }
        }
        
//方法三   用增强for循环遍历
public class MapMapDemo4 {
    public static void main(String[] args) {
        HashMap<String,String> java=new HashMap<String,String>();
        java.put("001","小明");
        java.put("002","小红");
        
        HashMap<String,String> javahoodp=new HashMap<String,String>();
        javahoodp.put("001", "张三");
        javahoodp.put("002","李四");
        
        HashMap<String,HashMap<String,String>> aaa=new HashMap<String,HashMap<String,String>>();
        aaa.put("java", java);
        aaa.put("javahoodp",javahoodp);
        
        Set<Entry<String, HashMap<String, String>>> entrySet = aaa.entrySet();
        for(Entry<String, HashMap<String, String>> i : entrySet){
            String akey = i.getKey();
            HashMap<String, String> avalue= i.getValue();
            //System.out.println(akey);
            //System.out.println(avalue);
            System.out.println(i);
            Set<Entry<String, String>> entrySet2 = avalue.entrySet();
            for(Entry<String, String> s : entrySet2){
                System.out.println(s);
            }
        }
        

 

转载于:https://www.cnblogs.com/qurui1998/p/10580819.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值