When to use the System.identityhashcode () and hashcode () method?

When to use the System.identityhashcode () and hashcode () method? *

+8
java hashcode
source share
5 answers

According to javadoc System.identityHashCode(Object o) :

Returns the same hash code for this object, which will be returned by the hashCode () method by default, regardless of whether the given class overrides the hashCode () object. The hash code for the null reference is zero.

So, in the first place, System.identityHashCode(nullReference) will always give you 0 instead of nullReference.hashCode() , which will obviously give you a NullPointerException at runtime.

Let us, however, consider the following class:

 public class MysteriousOne { @Override public int hashCode() { return 0xAAAABBBB; } //override equals() and so on... } 

The class overrides hashCode() , which is fine, even if the hash code for each instance is the same, which, however, is not very good if you want to distinguish between the identifiers of several instances. Usually you try to output the .toString() method (which by default gives the class name followed by @ , followed by the hashCode() output), for example, to find out the real identity of the object, but in this case the output will be the same:

 MysteriousOne first = new MysteriousOne(); MysteriousOne second = new MysteriousOne(); System.out.println("First: " + first); System.out.println("Second: " + second); 

The conclusion will be:

 First: MysteriousOne@aaaabbbb Second: MysteriousOne@aaaabbbb 

Thus, the existence of such an implementation of hashCode() cannot be distinguished between the identities of several instances. System.identityHashCode() convenient here.

If you do

 System.out.println("First: " + System.identityHashCode(first)); System.out.println("Second: " + System.identityHashCode(second)); 

you will get two different numbers for different instances, even if hashCode() their class implementation returns a constant (in fact, the redefined implementation of hashCode() here will not be called at all, like javadoc):

 First: 366712642 Second: 1829164700 

In addition, you can pass primitives to System.identityHashCode(Object o) , since they will be placed in the corresponding shells:

 int i = 5; System.out.println(System.identityHashCode(i)); 

Additional Information:

  • How do Object#hashCode() and System#identityHashCode() ?
+2
source share

Some of your classes may override the hashcode() method on your system. For these objects, this method provides the hash code of the provided object, which will be returned from its final parent, java.lang.Object . Refer to the Java API for a description from the language developers.

I believe that use is when you want to create almost non-unique objects for some reason. When I say almost, there may be objects with the same hash code. In doing so, you would minimize the use of equals methods.

0
source share

From the documentation :

public static int identityHashCode (Object x)

Returns the same hash code for this object, which will be returned by the hashCode () method by default, regardless of whether the given class overrides the hashCode () object. The hash code for the null reference is zero.

Note that identityHashCode returns hashCode as implemented in the Object class, and this is not what you usually want, so you should just use yourObject.hashCode() .

Regarding whether there may be some use cases for this method, if you have an Objects array, you iterate through it, it may have null in it, and you need hashCode these objects using System.identityHashCode(obj) can save you one zero check, not much of the improvement.

0
source share

System.identityhashcode () always returns the default java.lang.Object implementation, even if the class for a specific object overrides this and computes a different hash code.

In some circumstances, you may need this definition: two objects o1 and o2 are considered equal if and only if o1 == o2. In normal implementations, two objects o1 and o2 are considered equal if and only if o1 == null? o2 == null: o1.equals (o2). If two objects are equal if and only if o1 == o2, they must have the same hash code. Therefore, you should use System.identityhashcode ().

Here are some code snippets I came across.


 private static int hash(Object x, int length) { int h = System.identityHashCode(x); // Multiply by -127, and left-shift to use least bit as part of hash return ((h << 1) - (h << 8)) & (length - 1); } 

IdentityHashMap.java

 public int hashCode() { return System.identityHashCode(instance); } @Override @SuppressWarnings("rawtypes") public boolean equals(final Object other) { return other instanceof IdentityWrapper && ((IdentityWrapper) other).instance == instance; } 

IdentityWrapper.java (from the Apache Commons 2 pool)

0
source share

Well, for starters, while the system, of course, has a hashCode() method (each object has one), I see little reason to call it. Or any chance to even get a System object to call it, since System is final , and the constructor is private . So it's probably "almost never" for hashCode() , I think.

So this leaves us with System.identityHashCode(Object) , which gives you just the default hash value for any object. In other words, you do not need this very often, because if you do not override the hashCode () method in your class, you will already get the same default return value for your objects (because the default hashCode () is effective when you did not override the method hashCode (), just System.identityHashCode(this) ). There may be some use cases when they are used, but they are quite rare.

-one
source share

All Articles