第三次学JAVA再学不好就吃翔(part94)--HashMap嵌套HashMap

学习笔记,仅供参考,有错必纠



HashMap嵌套HashMap


没啥好说的,直接看代码。


输入:

package com.guiyang.bean;

import java.util.HashMap;

public class Demo8_HashMapHashMap {

	public static void main(String[] args) {
		HashMap<People, String> c1 = new HashMap<>();
		
		c1.put(new People("张三", 23), "北京");
		c1.put(new People("李四", 26), "广州");
		c1.put(new People("王五", 25), "深圳");
		
		HashMap<People, String> c2 = new HashMap<>();
		
		c2.put(new People("Ada", 21), "厦门");
		c2.put(new People("Jack", 20), "南京");
		c2.put(new People("Petter", 26), "广州");
		c2.put(new People("Black", 20), "香港");
		
		HashMap<HashMap<People, String>, String> c = new HashMap<>();
		c.put(c1, "一班");
		c.put(c2, "二班");
		
		for (HashMap<People, String> hashMap : c.keySet()) {
			for (People people : hashMap.keySet()) {
				System.out.println(people + " = " + hashMap.get(people) + c.get(hashMap));
			}
		}
	}
}

输出:

People [name=张三, age=23] = 北京一班
People [name=李四, age=26] = 广州一班
People [name=王五, age=25] = 深圳一班
People [name=Petter, age=26] = 广州二班
People [name=Black, age=20] = 香港二班
People [name=Ada, age=21] = 厦门二班
People [name=Jack, age=20] = 南京二班

猜你喜欢

转载自blog.csdn.net/m0_37422217/article/details/107445710