Collections

Java Array hashCode() method Code With Examples

Table of Contents

Program

import java.util.Arrays;
public class ArrayHashCode
{
   public static void main(String[] args) 
  {
      int[] iva = new int[] { 3, 5 };
      int ret = iva.hashCode();
      System.out.println("The hash code of value1 is: " + ret);
      iva= new int[] { 19, 75 };
      ret = iva.hashCode();
      System.out.println("The hash code of value2 is: " + ret);
   }
}

Output

The hash code of value1 is: 123961122
The hash code of value2 is: 942731712

Description

public static int hashCode​(int[] a)

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).
The value returned by this method is the same value that would be obtained by invoking the hashCode method on a List containing a sequence of Integer instances representing the elements of a in the same order. If a is null, this method returns 0.

Parameters:

a – the array whose hash value to compute

Returns:

a content-based hash code for a