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


The hashCode(int[]) method of the java.util.Arrays class returns a hash code based on the contents of the specified array. For any two non-null int 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) {
      int[] ival = new int[] { 3, 5 };
      int retval = ival.hashCode();
      System.out.println("The hash code of value1 is: " + retval);
      ival = new int[] { 19, 75 };
      retval = ival.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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 20-Feb-2020

57 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements