//集合嵌套
class Student1{
private String name;
private int age;
Student1(String name,int age){
this.name = name;
this.age=age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}
public class HashMapDemo {
public static void main(String args[]){
method_MapList();
}
public static void method_MapList(){
HashMap<String,ArrayList<Student1>> hmx = new HashMap<String,ArrayList<Student1>>();
ArrayList<Student1> gk = new ArrayList<Student1>();
gk.add(new Student1("zhou01",25));
gk.add(new Student1("zhou02",24));
gk.add(new Student1("zhou03",23));
gk.add(new Student1("zhou04",22));
gk.add(new Student1("zhou05",21));
ArrayList<Student1> rc = new ArrayList<Student1>();
rc.add(new Student1("fei01",25));
rc.add(new Student1("fei02",24));
rc.add(new Student1("fei03",23));
rc.add(new Student1("fei04",22));
rc.add(new Student1("fei05",21));
hmx.put("过控131", gk);
hmx.put("软测131", rc);
Iterator<Entry<String, ArrayList<Student1>>> it = hmx.entrySet().iterator();
while(it.hasNext()){
Entry<String,ArrayList<Student1>> en =it.next();
ArrayList<Student1> value =en.getValue();
System.out.println("班级:"+en.getKey());
method_getList(value);
}
}
public static void method_getList(ArrayList<Student1> al){
Iterator<Student1> it=al.iterator();
while(it.hasNext()){
Student1 stu =it.next();
System.out.println("姓名: "+stu.getName()+" 年龄:"+stu.getAge());
}
}
//集合套集合MapMap
public static void method_MapMap(){
HashMap<String,HashMap<String,Integer>> hmx = new HashMap<String,HashMap<String,Integer>>();
HashMap<String ,Integer> gk = new HashMap<String ,Integer>();
gk.put("zhou01", 27);
gk.put("zhou02", 26);
gk.put("zhou03", 25);
gk.put("zhou04", 24);
gk.put("zhou05", 23);
HashMap<String ,Integer> rc = new HashMap<String ,Integer>();
rc.put("fei01", 23);
rc.put("fei02", 22);
rc.put("fei03", 18);
rc.put("fei05", 19);
rc.put("fei04", 21);
rc.put("fei06", 20);
hmx.put("过控131", gk);
hmx.put("软测131", rc);
Iterator<Entry<String, HashMap<String, Integer>>> it = hmx.entrySet().iterator();
while(it.hasNext()){
Entry<String, HashMap<String, Integer>> en =it.next();
HashMap<String, Integer> value =en.getValue();
System.out.println("班级:"+en.getKey());
method_getMap(value);
}
}
public static void method_getMap(HashMap<String, Integer> hm){
Set<String> set = hm.keySet();
Iterator<String> it =set.iterator();
while(it.hasNext()){
String key =it.next();
Integer value =hm.get(key);
System.out.println("姓名: "+key+" 年龄:"+value);
}
}