When looking at different override options, hashCode()I was directed to Objects.hashCode(Object[])in the Google guava libraries ( javadoc ). The javadoc states that it is delegating Arrays.hashCode(Object[]). Is it safe to use this method in many different types of objects? Isn't that prone to hash collisions, or is it unlikely just because containers usually only contain one type of object?
As a simple example, consider the following classes:
public class Student {
private final String name;
public Student(String name) {
this.name = name;
}
@Override
public int hashCode() {
return Objects.hashCode(name);
}
}
public class Teacher {
private final String name;
public Teacher(String name) {
this.name = name;
}
@Override
public int hashCode() {
return Objects.hashCode(name);
}
}
public class HashCodeDriver {
public static void main(String[] args) {
final String name = "moe";
Student s = new Student(name);
Teacher t = new Teacher(name);
long studentHash = s.hashCode();
long teacherHash = t.hashCode();
System.out.println("studentHash=" + studentHash + " teacherHash=" + teacherHash);
if(studentHash == teacherHash) {
System.out.println("hash codes match");
}
else {
System.out.println("hash codes don't match");
}
}
}
Conclusion:
studentHash=108322 teacherHash=108322
hash codes match
Objects are two different types, but they generate the same hash code. Isn't that a problem? Should I pass in the class as the first parameter to prevent this collision? For instance,
public class Student {
private final String name;
public Student(String name) {
this.name = name;
}
@Override
public int hashCode() {
return Objects.hashCode(Student.class, name);
}
}
public class Teacher {
private final String name;
public Teacher(String name) {
this.name = name;
}
@Override
public int hashCode() {
return Objects.hashCode(Teacher.class, name);
}
}
javadoc ? javadoc,
. , - - .