java – guava-libraries:Objects. Is hashcode (object []) collision safe?

When looking at the different options for overriding hashcode (), I was directed to objects. In Google's guava libraries (Javadoc) hashCode(Object []). Javadoc declares that it delegates to arrays hashCode(Object []). Is it safe to use this method in many different object types? Is this not prone to hash conflicts, or is this not just because containers usually contain only one type of object?

For 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");
        }
    }
}

Output:

studentHash=108322 teacherHash=108322
hash codes match

Objects are two different types, but generate the same hash code Isn't that a problem? Should I pass the class as the first parameter to prevent this conflict? For example,

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);
    }
}

Is that why the Javadoc warning only provides a single object for this method? From Javadoc,

Solution

This is not a problem when two different objects of two different types have the same hash code

Hopefully, when you plan to build a HashMap, you won't take students and teachers as the key to the map Even if you want to do HashMap < object, Object > You'll be fine because

assertFalse( new Teacher( "John Smith" ).equals( new Student( "John Smith" ) );

This is an important reason for rewriting hashcode and equals

Entrusted to arrays The only disadvantage of hashcode (object []) may be that sometimes it may be too expensive from a performance point of view

For example, in your case, this would be a better hash method for teachers or students

@Override
public int hashCode() {
    return name.hashCode();
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>