Here you can find the source of deepHashCode(Object a[])
Parameter | Description |
---|---|
a | the array whose deep-content-based hash code to compute |
public static int deepHashCode(Object a[])
//package com.java2s; /*/*from w w w .j a v a 2s.co m*/ * Copyright 2016 xueyi (1581249005@qq.com) * * The Smart-Actor Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ public class Main { /** * Returns a hash code based on the "deep contents" of the specified * array. If the array contains other arrays as elements, the * hash code is based on their contents and so on, ad infinitum. * It is therefore unacceptable to invoke this method on an array that * contains itself as an element, either directly or indirectly through * one or more levels of arrays. The behavior of such an invocation is * undefined. * * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that * <tt>Arrays.deepEquals(a, b)</tt>, it is also the case that * <tt>Arrays.deepHashCode(a) == Arrays.deepHashCode(b)</tt>. * * <p>The computation of the value returned by this method is similar to * that of the value returned by {@link List#hashCode()} on a list * containing the same elements as <tt>a</tt> in the same order, with one * difference: If an element <tt>e</tt> of <tt>a</tt> is itself an array, * its hash code is computed not by calling <tt>e.hashCode()</tt>, but as * by calling the appropriate overloading of <tt>Arrays.hashCode(e)</tt> * if <tt>e</tt> is an array of a primitive type, or as by calling * <tt>Arrays.deepHashCode(e)</tt> recursively if <tt>e</tt> is an array * of a reference type. If <tt>a</tt> is <tt>null</tt>, this method * returns 0. * * @param a the array whose deep-content-based hash code to compute * @return a deep-content-based hash code for <tt>a</tt> * @see #hashCode(Object[]) * @since 1.5 */ public static int deepHashCode(Object a[]) { if (a == null) return 0; int result = 1; for (Object element : a) { int elementHash = 0; if (element instanceof Object[]) elementHash = deepHashCode((Object[]) element); else if (element instanceof byte[]) elementHash = hashCode((byte[]) element); else if (element instanceof short[]) elementHash = hashCode((short[]) element); else if (element instanceof int[]) elementHash = hashCode((int[]) element); else if (element instanceof long[]) elementHash = hashCode((long[]) element); else if (element instanceof char[]) elementHash = hashCode((char[]) element); else if (element instanceof float[]) elementHash = hashCode((float[]) element); else if (element instanceof double[]) elementHash = hashCode((double[]) element); else if (element instanceof boolean[]) elementHash = hashCode((boolean[]) element); else if (element != null) elementHash = element.hashCode(); result = 31 * result + elementHash; } return result; } /** * Returns a hash code based on the contents of the specified array. * For any two <tt>long</tt> arrays <tt>a</tt> and <tt>b</tt> * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. * * <p>The value returned by this method is the same value that would be * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} * method on a {@link List} containing a sequence of {@link Long} * instances representing the elements of <tt>a</tt> in the same order. * If <tt>a</tt> is <tt>null</tt>, this method returns 0. * * @param a the array whose hash value to compute * @return a content-based hash code for <tt>a</tt> * @since 1.5 */ public static int hashCode(long a[]) { if (a == null) return 0; int result = 1; for (long element : a) { int elementHash = (int) (element ^ (element >>> 32)); result = 31 * result + elementHash; } return result; } /** * Returns a hash code based on the contents of the specified array. * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt> * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. * * <p>The value returned by this method is the same value that would be * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} * method on a {@link List} containing a sequence of {@link Integer} * instances representing the elements of <tt>a</tt> in the same order. * If <tt>a</tt> is <tt>null</tt>, this method returns 0. * * @param a the array whose hash value to compute * @return a content-based hash code for <tt>a</tt> * @since 1.5 */ public static int hashCode(int a[]) { if (a == null) return 0; int result = 1; for (int element : a) result = 31 * result + element; return result; } /** * Returns a hash code based on the contents of the specified array. * For any two <tt>short</tt> arrays <tt>a</tt> and <tt>b</tt> * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. * * <p>The value returned by this method is the same value that would be * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} * method on a {@link List} containing a sequence of {@link Short} * instances representing the elements of <tt>a</tt> in the same order. * If <tt>a</tt> is <tt>null</tt>, this method returns 0. * * @param a the array whose hash value to compute * @return a content-based hash code for <tt>a</tt> * @since 1.5 */ public static int hashCode(short a[]) { if (a == null) return 0; int result = 1; for (short element : a) result = 31 * result + element; return result; } /** * Returns a hash code based on the contents of the specified array. * For any two <tt>char</tt> arrays <tt>a</tt> and <tt>b</tt> * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. * * <p>The value returned by this method is the same value that would be * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} * method on a {@link List} containing a sequence of {@link Character} * instances representing the elements of <tt>a</tt> in the same order. * If <tt>a</tt> is <tt>null</tt>, this method returns 0. * * @param a the array whose hash value to compute * @return a content-based hash code for <tt>a</tt> * @since 1.5 */ public static int hashCode(char a[]) { if (a == null) return 0; int result = 1; for (char element : a) result = 31 * result + element; return result; } /** * Returns a hash code based on the contents of the specified array. * For any two <tt>byte</tt> arrays <tt>a</tt> and <tt>b</tt> * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. * * <p>The value returned by this method is the same value that would be * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} * method on a {@link List} containing a sequence of {@link Byte} * instances representing the elements of <tt>a</tt> in the same order. * If <tt>a</tt> is <tt>null</tt>, this method returns 0. * * @param a the array whose hash value to compute * @return a content-based hash code for <tt>a</tt> * @since 1.5 */ public static int hashCode(byte a[]) { if (a == null) return 0; int result = 1; for (byte element : a) result = 31 * result + element; return result; } /** * Returns a hash code based on the contents of the specified array. * For any two <tt>boolean</tt> arrays <tt>a</tt> and <tt>b</tt> * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. * * <p>The value returned by this method is the same value that would be * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} * method on a {@link List} containing a sequence of {@link Boolean} * instances representing the elements of <tt>a</tt> in the same order. * If <tt>a</tt> is <tt>null</tt>, this method returns 0. * * @param a the array whose hash value to compute * @return a content-based hash code for <tt>a</tt> * @since 1.5 */ public static int hashCode(boolean a[]) { if (a == null) return 0; int result = 1; for (boolean element : a) result = 31 * result + (element ? 1231 : 1237); return result; } /** * Returns a hash code based on the contents of the specified array. * For any two <tt>float</tt> arrays <tt>a</tt> and <tt>b</tt> * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. * * <p>The value returned by this method is the same value that would be * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} * method on a {@link List} containing a sequence of {@link Float} * instances representing the elements of <tt>a</tt> in the same order. * If <tt>a</tt> is <tt>null</tt>, this method returns 0. * * @param a the array whose hash value to compute * @return a content-based hash code for <tt>a</tt> * @since 1.5 */ public static int hashCode(float a[]) { if (a == null) return 0; int result = 1; for (float element : a) result = 31 * result + Float.floatToIntBits(element); return result; } /** * Returns a hash code based on the contents of the specified array. * For any two <tt>double</tt> arrays <tt>a</tt> and <tt>b</tt> * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. * * <p>The value returned by this method is the same value that would be * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} * method on a {@link List} containing a sequence of {@link Double} * instances representing the elements of <tt>a</tt> in the same order. * If <tt>a</tt> is <tt>null</tt>, this method returns 0. * * @param a the array whose hash value to compute * @return a content-based hash code for <tt>a</tt> * @since 1.5 */ public static int hashCode(double a[]) { if (a == null) return 0; int result = 1; for (double element : a) { long bits = Double.doubleToLongBits(element); result = 31 * result + (int) (bits ^ (bits >>> 32)); } return result; } /** * Returns a hash code based on the contents of the specified array. If * the array contains other arrays as elements, the hash code is based on * their identities rather than their contents. It is therefore * acceptable to invoke this method on an array that contains itself as an * element, either directly or indirectly through one or more levels of * arrays. * * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that * <tt>Arrays.equals(a, b)</tt>, it is also the case that * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. * * <p>The value returned by this method is equal to the value that would * be returned by <tt>Arrays.asList(a).hashCode()</tt>, unless <tt>a</tt> * is <tt>null</tt>, in which case <tt>0</tt> is returned. * * @param a the array whose content-based hash code to compute * @return a content-based hash code for <tt>a</tt> * @see #deepHashCode(Object[]) * @since 1.5 */ public static int hashCode(Object a[]) { if (a == null) return 0; int result = 1; for (Object element : a) result = 31 * result + (element == null ? 0 : element.hashCode()); return result; } }