/*
* Map扩展知识。 Map集合被使用是因为具备映射关系。
* “yureban” “01” “zhangsan1”
* “yureban” “02” “zhangsan2”
* “jiuyeban” "01" "lisi1"
* “jiuyeban” "02" "lisi2" *
* 一个学校有多个教室,每个教室有自己的名称。
* 存入,取出,以及排序,数据唯一性等等综合练习
*
*/
import java.util.*;
class Student2
{
private String id;
private String name;
Student2(String id,String name)
{
this.id = id;
this.name= name;
}
public String toString()
{
return id+"+++"+name;
}
}
public class MapDemo3
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void demo()
{
HashMap<String,List<Student2>> czbk = new HashMap<String,List<Student2>>();
List<Student2> yure = new ArrayList<Student2>();
List<Student2> jiuye = new ArrayList<Student2>();
czbk.put("yureban", yure);
czbk.put("jiuyeban", jiuye);
yure.add(new Student2("01","zhang1"));
yure.add(new Student2("02","zhang2"));
jiuye.add(new Student2("03","li1"));
jiuye.add(new Student2("04","li2"));
Iterator<String> it =czbk.keySet().iterator();
while(it.hasNext())
{
String roomName = it.next();
List<Student2> room = czbk.get(roomName);
getInfos(room);
}
}
public static void getInfos(List<Student2> list)
{
Iterator<Student2> it= list.iterator();
while(it.hasNext())
{
Student2 stu = it.next();
sop(stu.toString());
}
}
public static void main(String[] args)
{
demo();
/*
HashMap<String,HashMap<String,String>> czbk = new HashMap<String,HashMap<String,String>>();
HashMap<String,String> yure = new HashMap<String,String>();
HashMap<String,String> jiuye = new HashMap<String,String>();
czbk.put("yureban",yure);
czbk.put("jiuyeban", jiuye);
yure.put("01","zhangsan1");
yure.put("02","zhangsan2");
jiuye.put("01","lisi1");
jiuye.put("02","lisi2");
//遍历czbk集合,获取所有教室
Iterator<String> it =czbk.keySet().iterator();
while(it.hasNext())
{
String roomName = it.next();
HashMap<String,String> room = czbk.get(roomName);
//sop(roomName);
getStudentInfo(room);
}*/
//getStudentInfo(yure);
}
public static void getStudentInfo(HashMap<String,String> roomMap)
{
Iterator<String> it = roomMap.keySet().iterator();
while(it.hasNext())
{
String id = it.next();
String name = roomMap.get(id);
sop(id + ":"+name);
}
}
}
|
|