自定义学生对象
public class Student {
private String name;
private Integer age;
public Student() {
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashmapQiantao {
public static void main(String[] args) {
HashMap<String, Integer> hashMap1 = new HashMap<>(); //基础班
hashMap1.put("张三",20);
hashMap1.put("李四",22);
HashMap<String, Integer> hashMap2 = new HashMap<>(); //就业班
hashMap2.put("王五",21);
hashMap2.put("赵六",23);
HashMap<String, HashMap<String, Integer>> hashMap3 = new HashMap<>();//班级类别
hashMap3.put("1班",hashMap1);
hashMap3.put("2班",hashMap2);
Set<Map.Entry<String, HashMap<String, Integer>>> entries = hashMap3.entrySet();
for (Map.Entry<String, HashMap<String, Integer>> mapEntry:entries
) {
String key = mapEntry.getKey();
System.out.println(key);
HashMap<String, Integer> value = mapEntry.getValue();
Set<Map.Entry<String, Integer>> entries1 = value.entrySet();
for (Map.Entry<String, Integer> entry: entries1) {
String key1 = entry.getKey();
Integer value1 = entry.getValue();
System.out.println("\t"+key1+" "+value1);
}
System.out.println();
}
}
}