集合嵌套

/*
集合的嵌套使用;
*/
package mapDemo4;
import java.util.*;
class MapDemo4 
{
	public static void main(String[] args) 
	{
		demo();
	}
	//HashMap裏面有List
	public static void demo()
	{
		HashMap<String,List<Student>> hm=new HashMap<String,List<Student>>();
		List<Student> room=new ArrayList<Student>();
		List<Student> room1=new ArrayList<Student>();
		hm.put("jichu",room);
		hm.put("gaoji",room1);
		room.add(new Student(001,"zs"));
		room.add(new Student(002,"ls"));
		room1.add(new Student(001,"ww"));
		room1.add(new Student(002,"zl"));
		getInfo1(hm);
	}
	//獲取HashMap的值
	public static void  getInfo1(HashMap<String,List<Student>> hs)
	{
		Set<String> s=hs.keySet();
		Iterator<String> it=s.iterator();
		while(it.hasNext())
		{
			String str=it.next();
			List<Student> stu=hs.get(str);
			getInfo(stu);
		}
	}
	//獲取list的值
	public static void getInfo(List<Student> list)
	{
       Iterator<Student> it=list.iterator();
	   while(it.hasNext())
	  {
		   Student stu=it.next();
		   System.out.println(stu);
	   }
	}
}
//學生類
class Student
{
	private int id;
	private String name;
	Student(int id,String name)
	{
		this.id=id;
		this.name=name;
	}
	public String toString()
	{
		return id+"::"+name;
	}
}

發佈了69 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章