class Student {
private String name;
private int age;
private char sex;
public Student(String name, int age, char sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + sex;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex != other.sex)
return false;
return true;
}
}
public class Nestificationlevel3 {
public static void main(String[] args) {
//第一个班
Map<String, Student> map1 = new HashMap< >();
map1.put("0x0001", new Student("漩涡鸣人",18,'男'));
map1.put("0x0002", new Student("宇智波佐助",20,'男'));
//第二个班
Map<String,Student> map2 = new HashMap<>();
map2.put("0C0001", new Student("宇智波鼬",25,'男'));
map2.put("0C0002", new Student("漩涡玖辛娜",23,'女'));
Map<String,Map<String, Student>> map = new HashMap<>();
map.put("卡卡西班", map1);
map.put(" 晓 班 ", map2);
//遍历!
Set<String> keys1 = map.keySet();
//对Set中的keys1进行遍历,也就是将map中的键进行遍历
for(String key1:keys1){
System.out.println(key1);
//取出map中的元素之后,把他装到一个Map集合m里
Map<String, Student> m = map.get(key1);
//对m集合的索引进行遍历,取出里面的元素
Set<String> keys2 = m.keySet();
for(String key2:keys2){
System.out.println("\t"+key2+"\t");
Student stu = m.get(key2);
System.out.println(stu.getName()+"------------"+stu.getAge()+"-------------"+stu.getSex());
}
}
}
} |
|