What does the method hashCode(obj[] a) do in java?



The hashCode(Object[]) method of the java.util.Arrays class returns a hash code based on the contents of the specified array. If the array contains other arrays of elements, the hash code is based on their identities rather than their contents. For any two arrays a and b such that Arrays.equals(a, b), it is also the case that Arrays.hashCode(a) == Arrays.hashCode(b).

Example

import java.util.Arrays;
   public class ArrayDemo {   
      public static void main(String[] args) {
      Object[] ob = new Object[] { 22, 7 };
      int retval = ob.hashCode();
      System.out.println("The hash code of value1 is: " + retval);
      
      ob = new Object[] { 3.5, 8.5 };
      retval = ob.hashCode();
      System.out.println("The hash code of value2 is: " + retval);
   }
}

Output

The hash code of value1 is: 4072869
The hash code of value2 is: 1671711
Sai Subramanyam
Sai Subramanyam

Passionate, Curious and Enthusiastic.


Advertisements