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);
}
}
}