Is it recommended to use hashcode to determine equality in Java?

Say we have a hashcode() function that will then be used inside our equals() method to determine the equality of two objects. Is this an acceptable / acceptable approach?

Suppose we use a simple hash code implementation. (For example, multiple instance variables multiplied by primes.)

+5
source share
4 answers

This is a terrible way to check for equality, mainly since objects do not have to return the same hash code.

You should always use the equals method to do this.

General rule:

If the equals method returns true for objects a and b, the hashCode method must return the same value for a and b.

This does not mean that if the hashCode method for a and b returns the same value, the equals method should return true for these two instances.

eg:

 public int hashCode(){ return 5; } 

is a valid, albeit ineffective, implementation of hashcode.

EDIT:

to use it in the equals method would be something like this:

 public class Person{ private String name; public Person(String name){ this.name = name;} public String getName(){ return this.name;} @Override public boolean equals(Object o){ if ( !(o instanceof Person)){ return false;} Person p = (Person)o; boolean nameE = this.name == null ? p.getName() == null : this.name.equals(p.getName()); boolean hashE = nameE ? true : randomTrueOrFalse(); // the only moment you're sure hashE is true, is if the previous check returns true. // in any other case, it doesn't matter whether they are equal or not, since the nameCheck returns false, so in best case, it redundant return nameE && hashE; } @Override public int hashCode(){ int hash = generateValidHashCode(); return hash; } } 
+8
source

This is a very bad practice. It is assumed that hashes have a minimum number of collisions, but usually you have more opportunities for objects than the number of possible hashes, and due to the pigeonhole principle several separate objects should have the same hash.

When comparing hashes, you have a certain chance of getting "false positives."

+2
source

Actually, this is a good idea!

But make sure you use this method to determine inequality, not equality. Hashing code can be faster than checking for equality, especially when saving the hash code (for example, in java.lang.String ).

If two objects have different hash codes, they must be different, otherwise they can be the same. For example, you can use this method as follows

 Object a, b; if(a.hashCode() == b.hashCode()){ if(a.equals(b)) return true; } return false; 

Remember that in some cases, the code above may be slower than using only equals() , especially if in most cases a makes equal to b .

From the documentation of Object.java :

  • If two objects are equal in accordance with the equals(Object) method, then calling the hashCode method for each of the two objects should produce the same integer result.
  • It is not required that the two objects be unequal in the equals(java.lang.Object) method equals(java.lang.Object) , then a call to the hashCode method for each of the two objects should produce separate integer results. However, the programmer should be aware that creating separate integer results for unequal objects can improve the performance of hash tables.
+1
source

Do not do that

Although it is correct that you need to override equals () and hashCode () in pairs, the presence of the same hash does not match the same values.

Put some effort into really thinking about the existence of equality. Do not aim here , he will bite you later .

0
source

Source: https://habr.com/ru/post/1239334/


All Articles