Close

Java Collections - Arrays.hashCode() Examples

Java Collections Java Java API 


Class:

java.util.Arrays

java.lang.Objectjava.lang.Objectjava.util.Arraysjava.util.ArraysLogicBig

Methods:

These methods return a hash code based on the contents of the specified array.

public static int hashCode(long[] a)
public static int hashCode(int[] a)
public static int hashCode(short[] a)
public static int hashCode(char[] a)
public static int hashCode(byte[] a)
public static int hashCode(boolean[] a)
public static int hashCode(float[] a)
public static int hashCode(double[] a)
public static int hashCode(Object[] a)

Examples


package com.logicbig.example.arrays;

import java.util.Arrays;

public class HashCodeExample {

public static void main(String... args) {
int[] arr1 = {3, 5, 6, 7, 9};

//native
int h1 = arr1.hashCode();

//individual direct elements#hashCode, if element is array then native
int h2 = Arrays.hashCode(arr1);

System.out.println(h1);
System.out.println(h2);
}
}

Output

1388884979
31554661




For nested arrays we should use Arrays#deepHashcode()

package com.logicbig.example.arrays;

import java.util.Arrays;

public class HashCodeExample2 {

public static void main(String... args) {
Object[] arr1 = {3, 5, new int[]{6, 7, 9}};

//native
int h3 = arr1.hashCode();

//individual direct elements#hashCode, if element is array then native
int h2 = Arrays.hashCode(arr1);

//deep, recursive elements#hashcode
int h1 = Arrays.deepHashCode(arr1);

System.out.println(h1);
System.out.println(h2);
System.out.println(h3);
}
}

Output

68612
852797571
1319456297




See Also