In my project I have seen the performance issues if we include collection fields in hashCode() and equal() methods.Is it mandatory to include or not? Below is the sample program.
Student.java
public class Student {
int no;
String name;
String adress;
Set < Parent > parent;
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((adress == null) ? 0 :
adress.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + no;
result = prime * result + ((parent == null) ? 0 :
parent.hashCode());
return result;
}
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 (adress == null) {
if (other.adress != null)
return false;
} else if (!adress.equals(other.adress))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (no != other.no)
return false;
if (parent == null) {
if (other.parent != null)
return false;
} else if (!parent.equals(other.parent))
return false;
return true;
}
}