Map集合的嵌套

Map集合是具有保存映射关系的集合,以健值对的关系存在的。集合里保存这两种值,一种是Key值,另一种是Value值。
Map集合的嵌套是一个集合中嵌套里一个集合,也就是一个集合中有另外一个集合。
我下面写的是在18级找班级和班级的学生姓名和学生年龄。
我用的方法是通过Key值找Value值

package jihe;

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

public class Student {
	public String string;
	public int age;

	public Student(String string, int age) {
		this.string = string;
		this.age = age;

	}
	
	public String toString() {
		return "   " + string + "  " + age+ "";
	}


	public static void main(String[] args) {
		HashMap<String, HashMap<String, Student>> ha = new HashMap<>();//这个集合存放年级和学生信息
		HashMap<String, Student> ha1 = new HashMap<>();// 班级,姓名,年龄
		Student st1 = new Student("z", 23);
		Student st2 = new Student("q", 24);
		ha1.put("001", st1);//班级和学生信息
		ha1.put("002", st2);班级和学生信息
		ha.put("18", ha1);//年级和学生

		Set<String> se = ha.keySet();//得到Key
		for (String st : se) {
			System.out.println(st);
			HashMap<String, Student> value = ha.get(st);//通过Key值找Value值(学生信息)

			Set<String> se1 = value.keySet();//再通过Value值(学生信息)找班级和学生姓名年龄
			for (String str1 : se1) {
			Student stu = value.get(str1);
				System.out.println(str1 + "...." +stu);//打印键值队
			}

		}

	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值