package com.heima.test;
import java.util.HashMap;
import com.heima.Student.Student;
public class hashmaphashmap {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 公司龙套组
HashMap<Student, String> hm1 = new HashMap<>();
hm1.put(new Student("张三", 23), "北京");
hm1.put(new Student("李四", 24), "上海");
hm1.put(new Student("王五", 25), "深圳");
hm1.put(new Student("赵六", 26), "广州");
hm1.put(new Student("吴七", 27), "苏州");
// 公司大神组
HashMap<Student, String> hm2 = new HashMap<>();
hm2.put(new Student("黄粲迪", 18), "北京");
hm2.put(new Student("冯光", 24), "上海");
hm2.put(new Student("猪婆", 25), "深圳");
hm2.put(new Student("大臭脚", 26), "广州");
hm2.put(new Student("死猪", 27), "苏州");
// 定义公司盈利组(包括龙套和大神)
HashMap<HashMap<Student, String>, String> hm = new HashMap<>();
hm.put(hm1, "公司龙套组");
hm.put(hm2, "公司大神组");
// 遍历mapmap
for (HashMap<Student, String> c : hm.keySet()) { // hm.keySet()代表键中的集合
String vaule = hm.get(c); // 根据键对象获取值对象
// System.out.println(vaule);
for (Student s : c.keySet()) { // c.keySet()代表集合中所有学生键对象
String vaule1 = c.get(s); // 根据键对象获取值对象
System.out.println(s + "=" + vaule1 + "=" + vaule);
}
}
}
}
这是student类
package com.heima.Student;
public class Student {
private String name;
private int age;
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;
}
/**
* @param name
* @param age
*/
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
/**
*
*/
public Student() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
// TODO Auto-generated method stub
// return "Person : [name=" + name + ", age = " + age + "]";
return "Person : [name = " + name + ", age = " + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
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;
return true;
}
}