I have few issues/doubts to fill values in a HashMap
I want a HashMap to accept "Student" as key and "Details" as value. Since key to a hashMap should be immutable I have some doubts how can this be dealt if
- Student class did not cloneable
Student class has reference to which in turn have reference to "Lab"
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); } }
Now Even if Student is cloneable, I can get reference of Department and call setLab() which changes the StudentObject. (Am I wrong?)
Now if Department and Lab are from 3rd party jars, how can I use Student Object in my Map if Student hashCode is (primeNumber+Student.id+Department.id+Lab.id).hashcode() [just some weird case];