Java Array Hash Code hashCode(Object array)

Here you can find the source of hashCode(Object array)

Description

Hashes an array.

License

Open Source License

Parameter

Parameter Description
array must be an array of any type or null

Return

hashCode

Declaration

static int hashCode(Object array) 

Method Source Code


//package com.java2s;

import java.util.Arrays;

public class Main {
    /**/*from  w ww. ja  v  a2  s .  c  om*/
     * Hashes an array. Provides a deep hash code in the case of an object array that contains other arrays.
     * 
     * @param array must be an array of any type or {@code null}
     * @return hashCode
     */
    static int hashCode(Object array) {
        int hashCode;
        if (array == null) {
            hashCode = 0;
        } else if (array instanceof byte[]) {
            hashCode = Arrays.hashCode((byte[]) array);
        } else if (array instanceof short[]) {
            hashCode = Arrays.hashCode((short[]) array);
        } else if (array instanceof int[]) {
            hashCode = Arrays.hashCode((int[]) array);
        } else if (array instanceof long[]) {
            hashCode = Arrays.hashCode((long[]) array);
        } else if (array instanceof float[]) {
            hashCode = Arrays.hashCode((float[]) array);
        } else if (array instanceof double[]) {
            hashCode = Arrays.hashCode((double[]) array);
        } else if (array instanceof boolean[]) {
            hashCode = Arrays.hashCode((boolean[]) array);
        } else if (array instanceof char[]) {
            hashCode = Arrays.hashCode((char[]) array);
        }
        //This is true if array is any non-primitive array
        else if (array instanceof Object[]) {
            hashCode = Arrays.hashCode((Object[]) array);
        } else {
            throw new IllegalArgumentException(
                    "value provided is not array, class: " + array.getClass().getCanonicalName());
        }

        return hashCode;
    }
}

Related

  1. hashCode(byte[] bytes)
  2. hashCode(byte[] obj)
  3. hashCode(final Object[] array1)
  4. hashCode(Map a)
  5. hashCode(Object array)
  6. hashCode(String[] names, Object[] values)
  7. printArray(byte[] array, boolean withHash)