【HashMap 嵌套 HashMap】

package com.yjf.esupplier.common.test;

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

/**
 * @author shusheng
 * @description HashMap 嵌套 HashMap
 * @Email [email protected]
 * @date 2018/12/18 14:46
 */
public class HashMapDemo2 {

    public static void main(String[] args) {
        HashMap<String, HashMap<String, Integer>> doubleMap = new HashMap<String, HashMap<String, Integer>>();

        // 创建基础班集合对象
        HashMap<String, Integer> jcMap = new HashMap<String, Integer>();
        // 添加元素
        jcMap.put("陈玉楼", 20);
        jcMap.put("高跃", 22);
        // 把基础班添加到大集合
        doubleMap.put("jc", jcMap);

        // 创建就业班集合对象
        HashMap<String, Integer> jyMap = new HashMap<String, Integer>();
        // 添加元素
        jyMap.put("李杰", 21);
        jyMap.put("曹石磊", 23);
        // 把基础班添加到大集合
        doubleMap.put("jy", jyMap);

        //遍历集合
        Set<String> oneMapSet = doubleMap.keySet();
        for (String mapKey : oneMapSet) {
            System.out.println(mapKey);
            HashMap<String, Integer> mapValue = doubleMap.get(mapKey);
            Set<String> twoMapValueSet = mapValue.keySet();
            for (String mapValueKey : twoMapValueSet) {
                Integer mapValueValue = mapValue.get(mapValueKey);
                System.out.println("\t" + mapValueKey + "---" + mapValueValue);
            }
        }
    }

}

猜你喜欢

转载自www.cnblogs.com/zuixinxian/p/10341204.html