Java Arrays - hashCode(boolean[]) Method



Description

The Java Arrays hashCode(boolean[]) method returns a hash code based on the contents of the specified array. For any two boolean arrays a and b such that Arrays.equals(a, b), it is also the case that Arrays.hashCode(a) == Arrays.hashCode(b).

Declaration

Following is the declaration for java.util.Arrays.hashCode() method

public static int hashCode(boolean[] a)

Parameters

a − This is the array whose hash value to compute.

Return Value

This method returns a content-based hash code for a.

Exception

NA

Java Arrays hashCode(byte[]) Method

Description

The Java Arrays hashCode(byte[]) method returns a hash code based on the contents of the specified array. For any two byte arrays a and b such that Arrays.equals(a, b), it is also the case that Arrays.hashCode(a) == Arrays.hashCode(b).

Declaration

Following is the declaration for java.util.Arrays.hashCode() method

public static int hashCode(byte[] a)

Parameters

a − This is the array whose hash value to compute.

Return Value

This method returns a content-based hash code for a.

Exception

NA

Java Arrays hashCode(char[]) Method

Description

The Java Arrays hashCode(char[]) method returns a hash code based on the contents of the specified array. For any two char arrays a and b such that Arrays.equals(a, b), it is also the case that Arrays.hashCode(a) == Arrays.hashCode(b).

Declaration

Following is the declaration for java.util.Arrays.hashCode() method

public static int hashCode(char[] a)

Parameters

a − This is the array whose hash value to compute.

Return Value

This method returns a content-based hash code for a.

Exception

NA

Java Arrays hashCode(double[]) Method

Description

The Java Arrays hashCode(double[]) method returns a hash code based on the contents of the specified array. For any two double arrays a and b such that Arrays.equals(a, b), it is also the case that Arrays.hashCode(a) == Arrays.hashCode(b).

Declaration

Following is the declaration for java.util.Arrays.hashCode() method

public static int hashCode(double[] a)

Parameters

a − This is the array whose hash value to compute.

Return Value

This method returns a content-based hash code for a.

Exception

NA

Java Arrays hashCode(float[]) Method

Description

The Java Arrays hashCode(float[]) method returns a hash code based on the contents of the specified array. For any two float arrays a and b such that Arrays.equals(a, b), it is also the case that Arrays.hashCode(a) == Arrays.hashCode(b).

Declaration

Following is the declaration for java.util.Arrays.hashCode() method

public static int hashCode(float[] a)

Parameters

a − This is the array whose hash value to compute.

Return Value

This method returns a content-based hash code for a.

Exception

NA

Java Arrays hashCode(int[]) Method

Description

The Java Arrays hashCode(int[]) method returns a hash code based on the contents of the specified array. For any two int arrays a and b such that Arrays.equals(a, b), it is also the case that Arrays.hashCode(a) == Arrays.hashCode(b).

Declaration

Following is the declaration for java.util.Arrays.hashCode() method

public static int hashCode(int[] a)

Parameters

a − This is the array whose hash value to compute.

Return Value

This method returns a content-based hash code for a.

Exception

NA

Java Arrays hashCode(long[]) Method

Description

The Java Arrays hashCode(long[]) method returns a hash code based on the contents of the specified array. For any two long arrays a and b such that Arrays.equals(a, b), it is also the case that Arrays.hashCode(a) == Arrays.hashCode(b).

Declaration

Following is the declaration for java.util.Arrays.hashCode() method

public static int hashCode(long[] a)

Parameters

a − This is the array whose hash value to compute.

Return Value

This method returns a content-based hash code for a.

Exception

NA

Java Arrays hashCode(short[]) Method

Description

The Java Arrays hashCode(short[]) method returns a hash code based on the contents of the specified array. For any two short arrays a and b such that Arrays.equals(a, b), it is also the case that Arrays.hashCode(a) == Arrays.hashCode(b).

Declaration

Following is the declaration for java.util.Arrays.hashCode() method

public static int hashCode(short[] a)

Parameters

a − This is the array whose hash value to compute.

Return Value

This method returns a content-based hash code for a.

Exception

NA

Java Arrays hashCode(Object[]) Method

Description

The Java Arrays hashCode(Object[]) method returns a hash code based on the contents of the specified array. For any two Object arrays a and b such that Arrays.equals(a, b), it is also the case that Arrays.hashCode(a) == Arrays.hashCode(b).

Declaration

Following is the declaration for java.util.Arrays.hashCode() method

public static int hashCode(Object[] a)

Parameters

a − This is the array whose hash value to compute.

Return Value

This method returns a content-based hash code for a.

Exception

NA

Example 1

The following example shows the usage of Java Arrays hashcode() methods for boolean[], byte[], char[] and double[]. First, we've created four arrays of boolean, byte, char and double and then their hashcode is printed using Arrays hashCode() method.

package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {

      // initializing arrays
      boolean[] boolArr = new boolean[] { true, true };
      byte[] byteArr = new byte[] {10, 20 };
      char[] charArr = new char[] {'A', 'B' };
      double[] doubleArr = new double[] {10.0, 20.0 };

      // printing hash code value
      System.out.println("The hash code of boolean array is: " + Arrays.hashCode(boolArr));
      System.out.println("The hash code of byte array is: " + Arrays.hashCode(byteArr));
      System.out.println("The hash code of char array is: " + Arrays.hashCode(charArr));
      System.out.println("The hash code of double array is: " + Arrays.hashCode(doubleArr));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

The hash code of boolean array is: 40353
The hash code of byte array is: 1291
The hash code of char array is: 3042
The hash code of double array is: 76547009

Example 2

The following example shows the usage of Java Arrays hashcode() methods for float[], int[], long[] and short[]. First, we've created four arrays of float, int, long and short and then their hashcode is printed using Arrays hashCode() method.

package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {

      // initializing arrays
      float[] floatArr = new float[] { 10.0f, 20.f };
      int[] intArr = new int[] {10, 20 };
      long[] longArr = new long[] { 10L, 20L };
      short[] shortArr = new short[] {10, 20 };

      // printing hash code value
      System.out.println("The hash code of float array is: " + Arrays.hashCode(floatArr));
      System.out.println("The hash code of int array is: " + Arrays.hashCode(intArr));
      System.out.println("The hash code of long array is: " + Arrays.hashCode(longArr));
      System.out.println("The hash code of short array is: " + Arrays.hashCode(shortArr));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

The hash code of float array is: 612369345
The hash code of int array is: 1291
The hash code of long array is: 1291
The hash code of short array is: 1291

Example 3

The following example shows the usage of Java Arrays hashcode() methods for Object[]. First, we've created an array of Student object and then their hashcode is printed using Arrays hashCode() method.

package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {

      // initializing array
      Student[] students = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };

      // printing hash code value
      System.out.println("The hash code of Object array is: " + Arrays.hashCode(students));
   }
}
class Student {
   int rollNo;
   String name;
   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

The hash code of Object array is: -1497234753
java_util_arrays.htm
Advertisements