java中的HashMap多层嵌套

 更新时间:2022年09月29日 09:53:20   投稿:jingxian  
这篇文章主要介绍了java中的HashMap多层嵌套问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

java HashMap多层嵌套

package chapter12;
import java.util.HashMap;
public class Demo03 {
    //班级  -组  -学生
    static HashMap<String,HashMap<String,HashMap<String,Integer>>> map= new HashMap<>();
    public static void main(String[] args) {
        HashMap<String,HashMap<String,Integer>> map50= new HashMap<>();
        HashMap<String,Integer> map50_1= new HashMap<>();
        map50_1.put("50_1_3",77);
        map50_1.put("50_1_2",88);
        map50_1.put("50_1_1",99);
        map50.put("第一组",map50_1);
        HashMap<String,Integer> map50_2= new HashMap<>();
        map50_2.put("50_2_3",75);
        map50_2.put("50_2_2",85);
        map50_2.put("50_2_1",95);
        map50.put("第二组",map50_2);
        map.put("通达50班",map50);
        HashMap<String,HashMap<String,Integer>> map51= new HashMap<>();
        HashMap<String,Integer> map51_1= new HashMap<>();
        map51_1.put("51_1_3",77);
        map51_1.put("51_1_2",88);
        map51_1.put("51_1_1",99);
        map51.put("第一组",map51_1);
        HashMap<String,Integer> map51_2= new HashMap<>();
        map51_2.put("51_2_3",75);
        map51_2.put("51_2_2",85);
        map51_2.put("51_2_1",95);
        map51.put("第二组",map51_2);
        map.put("通达51班",map51);
        HashMap<String,HashMap<String,Integer>> map52= new HashMap<>();
        HashMap<String,Integer> map52_1= new HashMap<>();
        map52_1.put("52_1_3",77);
        map52_1.put("52_1_2",88);
        map52_1.put("52_1_1",99);
        map52.put("第一组",map52_1);
        HashMap<String,Integer> map52_2= new HashMap<>();
        map52_2.put("52_2_3",75);
        map52_2.put("52_2_2",85);
        map52_2.put("52_2_1",95);
        map52.put("第二组",map52_2);
        map.put("通达52班",map52);
        map.forEach((className,cMap)->{
            System.out.println(className+"信息展示:");
            System.out.println("*******************************");
            cMap.forEach((group,gMap)->{
                System.out.println(className+group);
                gMap.forEach((name,score)->{
                    System.out.println("姓名:"+name+"\t分数:"+score);
                });
            });
            System.out.println("*****************************");
        });
//        forEach();
    }
//    public static void forEach(){
//        map.forEach((className,cMap)->{
//            System.out.println(className+"信息展示:");
//            System.out.println("*******************************");
//            cMap.forEach((group,gMap)->{
//                System.out.println(className+group);
//                gMap.forEach((name,score)->{
//                    System.out.println("姓名:"+name+"\t分数:"+score);
//                });
//            });
//            System.out.println("*****************************");
//        });
//    }
}

三层HashMap的嵌套

package cn.itcast_04;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
/*
 * 三层HashMap的嵌套
 * 
 * 动漫小说
 *    dm   动漫区
 *               rm  日漫:
 *                        漩涡鸣人        27
 *                        宇智波佐助      30
 *               gm  国漫:
 *                        闻人翊          29
 *                        张楚岚          29
 *    xs   小说区
 *               xh  玄幻:
 *                        狠人大帝      25
 *                        林动          32
 *               qh   奇幻
 *                         唐三       21
 *                         比比东     42
 */
public class 集合的多层嵌套 {
	public static void main(String[] args) {
		
		//创建集合1     
		HashMap<String,HashMap<String,ArrayList<Student>>> dx  = 
								new HashMap<String,HashMap<String,ArrayList<Student>>>();
		
		//创建动漫区
		 HashMap<String,ArrayList<Student>> dm = new   HashMap<String,ArrayList<Student>> ();
		   //创建日漫集合
		     ArrayList<Student> array1 = new ArrayList<Student>();
		        //创建并添加数据
			    Student s1 = new Student("漩涡鸣人 ",27);
				Student s2 = new Student("宇智波佐助 ",30);
		        array1.add(s1);
		        array1.add(s2);
		  //创建国漫集合
		    ArrayList<Student> array2 = new ArrayList<Student>();
		       //创建并添加数据
		       Student s3 = new Student("闻人翊",29);
			   Student s4 = new Student("张楚岚  ",29);
			   array2.add(s3);
			   array2.add(s4);
		 //添加
			   dm.put("日漫", array1);
			   dm.put("国漫", array2);
		
			   
	   //创建小说区
			HashMap<String,ArrayList<Student>> xs = new   HashMap<String,ArrayList<Student>>();
	     //创建玄幻集合
			ArrayList<Student> array3 = new ArrayList<Student>();
			   //创建并添加数据
			  Student s5 = new Student("狠人大帝 ",25);
			  Student s6 = new Student("林动  ",32);
			   array3.add(s5);
			   array3.add(s6);
			   //创建奇幻集合
			   ArrayList<Student> array4 = new ArrayList<Student>();
			   //创建并添加数据
			    Student s7 = new Student("唐三 ",21);
				Student s8 = new Student("比比东 ",42);
			    array4.add(s7);
				array4.add(s8);	
		//添加
				xs.put("玄幻", array3);
				xs.put("奇幻", array4);
				
				dx.put("动漫", dm);
				dx.put("小说", xs);
				
				
		//遍历
				Set<String>  hmset=  dx.keySet();
				for(String hmkey:hmset) {
					System.out.println(hmset);
					HashMap<String,ArrayList<Student>> hmvalue = dx.get(hmkey);
					    Set<String>  hm1set=  hmvalue.keySet();
					for(String hm1key:hm1set) {
						System.out.println("\t"+hm1set);
						  ArrayList<Student> value = hmvalue.get(hm1key);
						 for(Student s:value) {
							 System.out.println("\t\t"+s.getName()+"-------"+s.getAng());
						 }
					}
				}						     		    
	}
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • java中多个@Scheduled定时器不执行的解决方法

    java中多个@Scheduled定时器不执行的解决方法

    在应用开发中经常需要一些周期性的操作,比如每5分钟执行某一操作等,这篇文章主要给大家介绍了关于java中多个@Scheduled定时器不执行的解决方法,需要的朋友可以参考下
    2023-04-04
  • Java jwt使用公钥字符串验证解析token锁方法详解

    Java jwt使用公钥字符串验证解析token锁方法详解

    关于java获取Token验证的问题相信很多人都遇见过,尤其是对刚接触微信开发的人来说确实有点棘手,下面这篇文章主要给大家介绍了关于Java中token验证解析的相关资料,需要的朋友可以参考下
    2023-02-02
  • spring cloud eureka注册原理-注册失败填坑笔记

    spring cloud eureka注册原理-注册失败填坑笔记

    这篇文章主要介绍了spring cloud eureka注册原理-注册失败填坑笔记,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Java中值传递的深度分析

    Java中值传递的深度分析

    这篇文章主要给大家介绍了关于Java中值传递的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • SpringBoot+SpringSession+Redis实现session共享及唯一登录示例

    SpringBoot+SpringSession+Redis实现session共享及唯一登录示例

    这篇文章主要介绍了SpringBoot+SpringSession+Redis实现session共享及唯一登录示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Redisson RedLock红锁加锁实现过程及原理

    Redisson RedLock红锁加锁实现过程及原理

    本文主要介绍了Redis中Redisson红锁(Redlock)使用原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • SpringMVC的注解@RequestMapping属性及使用

    SpringMVC的注解@RequestMapping属性及使用

    这篇文章主要为大家介绍了SpringMVC注解@RequestMapping属性及使用,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • spring的几个重要类和接口(详解)

    spring的几个重要类和接口(详解)

    下面小编就为大家带来一篇spring的几个重要类和接口(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • 高斯混合模型与EM算法图文详解

    高斯混合模型与EM算法图文详解

    高斯模型就是用高斯概率密度函数(正态分布曲线)精确地量化事物,将一个事物分解为若干的基于高斯概率密度函数(正态分布曲线)形成的模型
    2021-08-08
  • 详解SpringBoot 处理异常的几种常见姿势

    详解SpringBoot 处理异常的几种常见姿势

    这篇文章主要介绍了详解SpringBoot 处理异常的几种常见姿势,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08

最新评论