class Student {
private String id;
private String name;
public Student() {
super();
}
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
HashMap嵌套HashMap
public class HashMapTest {
public static void main(String[] args) {
// 1: 创建基础班集合 HashMap<String, String>
HashMap<String, String> base = new HashMap<String, String>();
// 2: 向基础班集合添加元素
base.put("01", "陈如水");
base.put("02", "左帅");
//3: 创建就业班集合 HashMap<String, String>
HashMap<String, String> job = new HashMap<String, String>();
//4: 向就业班集合添加元素
job.put("01", "李天成");
job.put("02", "许伟");
//5: 创建一个czbk集合 HashMap< String, HashMap<String, String> >
HashMap<String, HashMap<String, String>> czbk = new HashMap<String, HashMap<String, String>>();
//6: 向czbk集合中 添加班级元素
czbk.put("基础班", base);
czbk.put("就业班", job);
//7: 遍历
//遍历学校集合, 得到每一个班级
Set<Entry<String, HashMap<String, String>>> classes = czbk.entrySet();
// 得到每一个班级
for (Entry<String, HashMap<String, String>> clazz : classes) {
//获取班级名称
String name = clazz.getKey();
System.out.println(name);
//获取班级元素
HashMap<String, String> students = clazz.getValue();
//获取到每一个学生信息
Set<Entry<String, String>> studentSet = students.entrySet();
for (Entry<String, String> student : studentSet) {
//获取学生的学号
String id = student.getKey();
//获取学生的姓名
String n = student.getValue();
System.out.println("\t"+id+"..."+n);
}
}
}
}
HashMap嵌套ArrayList
public class HashMapTest {
public static void main(String[] args) {
//1: 创建基础班集合 ArrayList<Student>
ArrayList<Student> base = new ArrayList<Student>();
//2: 向基础班集合添加元素
base.add(new Student("01","陈如水"));
base.add(new Student("02","左帅"));
//3: 创建就业班集合 ArrayList<Student>
ArrayList<Student> job = new ArrayList<Student>();
//4: 向就业班集合添加元素
job.add(new Student("01","李天成"));
job.add(new Student("02","许伟"));
//5: 创建czbk学校集合 HashMap<String, ArrayList<Student> >
HashMap<String, ArrayList<Student>> czbk = new HashMap<String, ArrayList<Student>>();
//6: 把班级添加到 学校集合中
czbk.put("基础班", base);
czbk.put("就业班", job);
//7: 遍历
//获取到所有班级的名称
Set<String> classNames = czbk.keySet();
//获取到每一个班级名称
for (String className : classNames) {
System.out.println(className);
//通过班级名称,获取到班级学生信息
ArrayList<Student> Students = czbk.get(className);
//获取到每一个学生信息
for(Student s : Students){
System.out.println("\t"+s.getId()+"..."+s.getName());
}
}
}
}
ArrayList嵌套HashMap
public class ArrayListTest {
public static void main(String[] args) {
// 1: 创建基础班集合 HashMap<String, String>
HashMap<String, String> base = new HashMap<String, String>();
// 2: 向基础班集合添加元素
base.put("01", "陈如水");
base.put("02", "左帅");
//3: 创建就业班集合 HashMap<String, String>
HashMap<String, String> job = new HashMap<String, String>();
//4: 向就业班集合添加元素
job.put("01", "李天成");
job.put("02", "许伟");
//5:创建一个czbk集合 ArrayList<HashMap<String, String> >
ArrayList<HashMap<String, String> > czbk = new ArrayList<HashMap<String, String>>();
//6: 向czbk集合中 添加班级元素
czbk.add(base);
czbk.add(job);
//7: 遍历(难点)
//获取到每一个班级
for (HashMap<String, String> clazz : czbk) {
//获取到班级的所有学生
Set<Entry<String, String>> students = clazz.entrySet();
//获取到每一个学生
for (Entry<String, String> student : students) {
String id = student.getKey();
String name = student.getValue();
System.out.println(id +"--"+name );
}
}
}
}
|