Map容器一映射多

import java.util.*;
/*Map容器中一映射多。获取元素的方法*/
class Student
{
	private String name;
	private String id;
	Student(String name,String id)
	{
		this.name = name;
		this.id = id;
	}
	public String toString()
	{
		return id+"...."+name;
	}
}
class MapDemo 
{

	public static void main(String[] args) 
	{
		HashMap<String,List<Student>> nbut = new HashMap<String,List<Student>>();

		List<Student> net = new ArrayList<Student>();
		List<Student> net2 = new ArrayList<Student>();

		nbut.put("net1",net);	//添加
		nbut.put("net2",net2);
		net.add(new Student("zhangsan","01"));
		net.add(new Student("zhangsan","04"));
		net2.add(new Student("zhangsan","02"));
		net2.add(new Student("zhangsan","03"));

		Iterator<String> it = nbut.keySet().iterator();
		while(it.hasNext())	//遍历
		{
			String room = it.next();
			System.out.println(room);
			getInfo(nbut.get(room));

		}
		Demo();
	}
	public static void getInfo(List<Student> ls)
	{
		Iterator<Student> it = ls.iterator();
		while(it.hasNext())
		{
			Student id = it.next();
			System.out.println(id);
		}
	}
	public static void Demo()
	{
		HashMap<String,HashMap<String,String>> nbut = new HashMap<String,HashMap<String,String>>();
		HashMap<String,String> net = new HashMap<String,String>();
		HashMap<String,String> net2 = new HashMap<String,String>();

		nbut.put("net",net);
		nbut.put("net2",net2);
		net.put("zhangsan","04");
		net.put("zhangsan1","03");
		net2.put("zhangsan1","02");
		net2.put("zhangsan","01");

		Iterator<String> it = nbut.keySet().iterator(); 
		while(it.hasNext())
		{
			String room = it.next();
			System.out.println(room);
			getInfoDemo(nbut.get(room)); 
		}

	}
	public static void getInfoDemo(HashMap<String,String> room)
	{
		Iterator<String> it = room.keySet().iterator();
		while(it.hasNext())
		{
			String name = it.next();
			String id = room.get(name);
			System.out.println(name+"..."+id);
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值