集合嵌套之HashMap嵌套Hashmap

集合嵌套之HashMap嵌套Hashmap

需求: 定义大一年级 大一两个不同专业计算机和设计
思路:

  • 1.定义专业设计
  • 2.定义大一年级
  • 3.定义专业计算机
  • 4.遍历双列集合
  • 5.遍历键的双列集合对象
  • 6.输出结果

代码:

import java.util.HashMap;
/*需求:
* 定义大一年级 大一两个不同专业计算机和设计
* 思路: 定义专业计算机
* 1.定义专业设计
* 2.定义大一年级
* 3.遍历双列集合
* 4.遍历键的双列集合对象
* 5.输出结果
* */
public class HashMapHashMap {
    public static void main(String[] args) {
        // 定义专业计算机
        HashMap<Student,String> hmJ = new HashMap <> ();
        hmJ.put (new Student ("张三",23),"安徽");
        hmJ.put (new Student ("李四",23),"安庆");
        hmJ.put (new Student ("王者",23),"徽州");
        // 定义专业设计
        HashMap<Student,String> hmS = new HashMap <> ();
        hmJ.put (new Student ("方明",23),"安徽");
        hmJ.put (new Student ("何米",23),"安庆");
        hmJ.put (new Student ("胡度",23),"徽州");

        //定义大一年级
        HashMap<HashMap<Student,String>,String> hm = new HashMap<> ();
        hm.put (hmJ,"计算机专业");
        hm.put (hmS,"设计专业");

        //遍历双列集合
        for (HashMap<Student,String> h : hm.keySet ( )) {   //hm代表的是双列集合中的键的集合
          String value = hm.get (h);    //get(h)根据键值对象获取值对象
          //遍历键的双列集合对象
            for (Student key : h.keySet ()) {   //h.keySet()获取集合中所有学生键的对象
                String value2 = h.get (key);    //get(key)根据键值对象获取值对象
                System.out.println (key + "=" + value + "=" +value);
            }
        }
    }
}

结果:
Student{name='张三', age=23}=计算机专业=计算机专业
Student{name='何米', age=23}=计算机专业=计算机专业
Student{name='李四', age=23}=计算机专业=计算机专业
Student{name='方明', age=23}=计算机专业=计算机专业
Student{name='胡度', age=23}=计算机专业=计算机专业
Student{name='王者', age=23}=计算机专业=计算机专业

发布了55 篇原创文章 · 获赞 5 · 访问量 4171

猜你喜欢

转载自blog.csdn.net/qq_43654669/article/details/100161522