java中HashMap的嵌套用例

来源:互联网 发布:金蝶软件技术服务 编辑:程序博客网 时间:2024/05/22 15:06
package itcast05;import java.util.HashMap;import java.util.Set;/** * HashMap嵌套HashMap *  * @author lgt * */public class HashMapDemo {    public static void main(String[] args) {        // 创建集合对象        HashMap<String, HashMap<String, Integer>> czbkMap = new HashMap<String, HashMap<String, Integer>>();        // 创建基础班jc集合对象        HashMap<String, Integer> jcMap = new HashMap<String, Integer>();        // 添加元素        jcMap.put("石乐志", 22);        jcMap.put("芜湖大司马", 39);        // 把基础班jcMap添加到czbkMap集合中        czbkMap.put("jc", jcMap);        // 创建就业班jy集合对象        HashMap<String, Integer> jyMap = new HashMap<String, Integer>();        // 添加元素        jyMap.put("怎么说", 28);        jyMap.put("不存在的", 12);        // 把基础班jyMap添加到czbkMap集合中        czbkMap.put("jy", jyMap);        // 遍历czbkMap集合方式1 根据键查找值        // 获取所有的键        Set<String> czbkMapSet = czbkMap.keySet();        // 遍历键的集合,获取每一个键        for (String czbkMapKey : czbkMapSet) {            System.out.println("班级类型:" + czbkMapKey);            // 根据键查找值            HashMap<String, Integer> czbkMapValue = czbkMap.get(czbkMapKey);            // 获取所有的键jcMap,jyMap            Set<String> jxMapSet = czbkMapValue.keySet();            for (String jxMapSetKey : jxMapSet) {                // 根据键查找值                int age = czbkMapValue.get(jxMapSetKey);                // System.out.println("班级类型:" + czbkMapKey + ", 姓名:" +                // jxMapSetKey + ", 年龄:" + age);                System.out.println("\t\t" + "姓名:" + jxMapSetKey + ", 年龄:" + age);            }        }    }}
package itcast05;import java.util.ArrayList;import java.util.HashMap;import java.util.Set;/** * ArrayList集合嵌套HashMap集合并遍历 *  * @author lgt * */public class HashMapIncludeArrayListDemo {    public static void main(String[] args) {        // 创建集合对象        ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();        // 创建元素1        HashMap<String, String> hm1 = new HashMap<String, String>();        hm1.put("周瑜", "小乔");        hm1.put("吕布", "貂蝉");        // 把元素添加到array里面        array.add(hm1);        // 创建元素2        HashMap<String, String> hm2 = new HashMap<String, String>();        hm2.put("郭靖", "黄蓉");        hm2.put("杨过", "小龙女");        // 把元素添加到array里面        array.add(hm2);        // 创建元素3        HashMap<String, String> hm3 = new HashMap<String, String>();        hm3.put("令狐冲", "任盈盈");        hm3.put("林平之", "岳灵珊");        // 把元素添加到array里面        array.add(hm3);        // 遍历        for (HashMap<String, String> hm : array) {            Set<String> set = hm.keySet();            for (String key : set) {                String value = hm.get(key);                System.out.println(key + "---" + value);            }        }    }}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 婴儿鼻泪管阻塞怎么办 孕妇流清水鼻涕怎么办 鼻涕是金黄的 怎么办 宝宝流鼻涕有痰怎么办 宝宝老是咳嗽流鼻涕怎么办 借钱逾期不还怎么办 借钱遇到老赖怎么办 如果造事者拒不执行赔偿怎么办? 遇到新疆暴徒来怎么办 隆胸一年下垂了怎么办 自动雨伞坏了怎么办 手机验证码搞忘了怎么办 uplay 登录不上怎么办 滴滴账号永久封禁怎么办 steam游戏买不了怎么办 电脑登不了游戏怎么办 育碧帧数低怎么办 帐号被盗怎么办微信 育碧登录错误怎么办 头盔买小了怎么办 上海离职了社保怎么办 日本瓦斯没交怎么办 半自动咖啡机自动出水怎么办 父母太粘自己怎么办 铜耳环变黑了怎么办 秋田犬爱打架怎么办 僧人建设庙宇资金怎么办 wifi总是掉线怎么办 微博授权失败怎么办 新浪微博被官方封号怎么办 电磁波辐射重度异常怎么办 卖鞋没有生意怎么办 华为手机越来越卡怎么办 华为手机电越充越少怎么办 土炕有裂缝冒烟怎么办 农村土炕不好烧怎么办 夏天土炕不好烧怎么办 怎么办我是个肚脐控 联想笔记本电池显示损坏怎么办 电脑关机没反应怎么办 变色灯坏了怎么办