我几乎没有什么问题/疑问可以在 HashMap 中填充值
我希望 HashMap 接受“Student”作为键,“Details”作为值。由于 hashMap 的密钥应该是不可变的,我有一些疑问,如果
- 学生类不可克隆
学生类有参考,而后者又参考了“实验室”
public class Student { private String id; private String name; private Department dept; public Student(String id, String name, Department dept) { this.id=id; this.name=name; this.dept=dept; } public Department getDepartment() { return this.dept; } } public class Department { private String deptId; private Lab lab; public Department(String deptId, Lab lab) { this.deptId=deptId; this.lab=lab; } public void setLab(Lab lab) { this.lab=lab; } } public class Lab { private String labId; private String labName; public Lab(String labId, String labName) { this.labId=labId; this.labName=labName; } } public class StudentDetails { private String fatherName; private String address public StudentDetails(String fatherName, String address) { this.fatherName=fatherName; this.address=address; } } public class StudentMaintainer { public static void main(String[] args) { StudentDetails stDetails= new StudentDetails("John","Mumbai"); Lab lab= new Lab("100","CS"); Department dept= new Department("900", lab); Student st = new Student("3000",dept); Map<Student,StudentDetails> studentMaintainer= new ArrayList<>(); studentMaintainer.put(st,stDetails); } }
现在即使 Student 是可克隆的,我也可以获得 Department 的引用并调用 setLab() 来更改 StudentObject。(我错了吗?)
现在,如果 Department 和 Lab 来自 3rd 方 jar,如果 Student hashCode 是 (primeNumber+Student.id+Department.id+Lab.id).hashcode() [只是一些奇怪的情况],我如何在我的地图中使用 Student 对象;