Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java hashcode based on identity

The default behavior of Object.hashCode() is to return essentially the "address" of the object so that a.hashCode() == b.hashCode() if and only if a == b. How can I get this behavior in a user-defined class if a superclass already defines hashCode()? For instance:

class A {
  public int hashCode() {
    return 0;
  }
}

class B extends A {
  public int hashCode() {
    // Now I want to return a unique hashcode for each object.
    // In pythonic terms, it'd look something like:
    return Object.hashCode(this);
  }
}

Ideas?

like image 968
hjfreyer Avatar asked Jun 30 '09 13:06

hjfreyer


People also ask

What is identity hashCode in Java?

identityHashCode() is the method which is used to return the same hash code for any given object that is returned by the default method hashCode(). Also, for every hash code with a null reference zero is returned.

How is hashCode determined in Java?

Hashcode in Java In general Hash Code is a number calculated by the hashCode() method of the Object class. Usually, programmers override this method for their objects as well as related to hashCode() the equals() method for more efficient processing of specific data.

Does hashCode need to be unique?

Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithms like hashtable, hashmap etc. An object can also be searched with this unique code. Returns: It returns an integer value which represents hashCode value for this Method.

What is the difference between hashCode and identityHashCode?

The hashcode() method is a non-final instance method, and should be overridden in any class where the equals(Object) is overridden. By contrast, identityHashCode(Object) is a static method and therefore cannot be overridden.


2 Answers

System.identityHashCode(Object) provides this behaviour.

You would write this:

class B extends A {
  public int hashCode() {
    return System.identityHashCode(this);
  }
}

Please check the equals-method, that it only returns true, if the two objects are the same. Otherwise it would break behaviour described for equals and hashCode. (To be correct, the equals-method has to return false, if you get different hashcodes for two objects.) To provide an implementation of equals() that comply with the given hashCode()-method:

public boolean equals(Object other){
   return this == other;
}
like image 178
Mnementh Avatar answered Oct 06 '22 17:10

Mnementh


Use System.identityHashCode(). This is what IdentityHashMap uses.

You should be extremely wary of overriding an existing hashCode() with this though because you might break the hashCode contract, being that two objects that:

if a.equals(b) then a.hashCode() must equal b.hashCode()

You might break this by overriding the existing behaviour or you might need to override equals() too.

like image 29
cletus Avatar answered Oct 06 '22 17:10

cletus