021_泛型嵌套
import java.util.HashMap;
import java.util.Map.Entry;public class ParameterizedType {public static void main(String[] args) {// 一個小學的學生, 學號對應學生HashMap<Integer, Student> primarySchool = new HashMap<>();primarySchool.put(1, new Student(1, "zhagnsan"));primarySchool.put(2, new Student(2, "lisi"));primarySchool.put(3, new Student(3, "wangwu"));// 一個中學的學生, 學號對應學生HashMap<Integer, Student> highSchool = new HashMap<>();highSchool.put(1, new Student(1, "xiaocui"));highSchool.put(2, new Student(2, "xiaoming"));highSchool.put(3, new Student(3, "xiaohua"));// 一個地區的學校學生, 學校名稱對象學校學生HashMap<String, HashMap<Integer, Student>> localSchool = new HashMap<>();localSchool.put("primarySchool", primarySchool);localSchool.put("highSchool", highSchool);// 這個地區的學校for (Entry<String, HashMap<Integer, Student>> schools : localSchool.entrySet()) {// 每個學校的學生for(Entry<Integer, Student> students : schools.getValue().entrySet()) {System.out.println(schools.getKey() + " " + students.getKey() + " " + students.getValue().name);}}}
}class Student{Integer id;String name;public Student(Integer id, String name) {this.id = id;this.name = name;}
}
?
總結
- 上一篇: 020_泛型变量的类型限定
- 下一篇: 005_MySQL数据类型