< prev index next >

src/java.base/share/classes/java/util/Arrays.java

Print this page




2698         if (aLength != bLength)
2699             return false;
2700 
2701         return ArraysSupport.mismatch(a, aFromIndex,
2702                                       b, bFromIndex,
2703                                       aLength) < 0;
2704     }
2705 
2706     /**
2707      * Returns {@code true} if the two specified arrays of shorts are
2708      * <i>equal</i> to one another.  Two arrays are considered equal if both
2709      * arrays contain the same number of elements, and all corresponding pairs
2710      * of elements in the two arrays are equal.  In other words, two arrays
2711      * are equal if they contain the same elements in the same order.  Also,
2712      * two array references are considered equal if both are {@code null}.
2713      *
2714      * @param a one array to be tested for equality
2715      * @param a2 the other array to be tested for equality
2716      * @return {@code true} if the two arrays are equal
2717      */
2718     public static boolean equals(short[] a, short a2[]) {
2719         if (a==a2)
2720             return true;
2721         if (a==null || a2==null)
2722             return false;
2723 
2724         int length = a.length;
2725         if (a2.length != length)
2726             return false;
2727 
2728         return ArraysSupport.mismatch(a, a2, length) < 0;
2729     }
2730 
2731     /**
2732      * Returns true if the two specified arrays of shorts, over the specified
2733      * ranges, are <i>equal</i> to one another.
2734      *
2735      * <p>Two arrays are considered equal if the number of elements covered by
2736      * each range is the same, and all corresponding pairs of elements over the
2737      * specified ranges in the two arrays are equal.  In other words, two arrays
2738      * are equal if they contain, over the specified ranges, the same elements


4455             return a[i];
4456         }
4457     }
4458 
4459     /**
4460      * Returns a hash code based on the contents of the specified array.
4461      * For any two {@code long} arrays {@code a} and {@code b}
4462      * such that {@code Arrays.equals(a, b)}, it is also the case that
4463      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4464      *
4465      * <p>The value returned by this method is the same value that would be
4466      * obtained by invoking the {@link List#hashCode() hashCode}
4467      * method on a {@link List} containing a sequence of {@link Long}
4468      * instances representing the elements of {@code a} in the same order.
4469      * If {@code a} is {@code null}, this method returns 0.
4470      *
4471      * @param a the array whose hash value to compute
4472      * @return a content-based hash code for {@code a}
4473      * @since 1.5
4474      */
4475     public static int hashCode(long a[]) {
4476         if (a == null)
4477             return 0;
4478 
4479         int result = 1;
4480         for (long element : a) {
4481             int elementHash = (int)(element ^ (element >>> 32));
4482             result = 31 * result + elementHash;
4483         }
4484 
4485         return result;
4486     }
4487 
4488     /**
4489      * Returns a hash code based on the contents of the specified array.
4490      * For any two non-null {@code int} arrays {@code a} and {@code b}
4491      * such that {@code Arrays.equals(a, b)}, it is also the case that
4492      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4493      *
4494      * <p>The value returned by this method is the same value that would be
4495      * obtained by invoking the {@link List#hashCode() hashCode}
4496      * method on a {@link List} containing a sequence of {@link Integer}
4497      * instances representing the elements of {@code a} in the same order.
4498      * If {@code a} is {@code null}, this method returns 0.
4499      *
4500      * @param a the array whose hash value to compute
4501      * @return a content-based hash code for {@code a}
4502      * @since 1.5
4503      */
4504     public static int hashCode(int a[]) {
4505         if (a == null)
4506             return 0;
4507 
4508         int result = 1;
4509         for (int element : a)
4510             result = 31 * result + element;
4511 
4512         return result;
4513     }
4514 
4515     /**
4516      * Returns a hash code based on the contents of the specified array.
4517      * For any two {@code short} arrays {@code a} and {@code b}
4518      * such that {@code Arrays.equals(a, b)}, it is also the case that
4519      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4520      *
4521      * <p>The value returned by this method is the same value that would be
4522      * obtained by invoking the {@link List#hashCode() hashCode}
4523      * method on a {@link List} containing a sequence of {@link Short}
4524      * instances representing the elements of {@code a} in the same order.
4525      * If {@code a} is {@code null}, this method returns 0.
4526      *
4527      * @param a the array whose hash value to compute
4528      * @return a content-based hash code for {@code a}
4529      * @since 1.5
4530      */
4531     public static int hashCode(short a[]) {
4532         if (a == null)
4533             return 0;
4534 
4535         int result = 1;
4536         for (short element : a)
4537             result = 31 * result + element;
4538 
4539         return result;
4540     }
4541 
4542     /**
4543      * Returns a hash code based on the contents of the specified array.
4544      * For any two {@code char} arrays {@code a} and {@code b}
4545      * such that {@code Arrays.equals(a, b)}, it is also the case that
4546      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4547      *
4548      * <p>The value returned by this method is the same value that would be
4549      * obtained by invoking the {@link List#hashCode() hashCode}
4550      * method on a {@link List} containing a sequence of {@link Character}
4551      * instances representing the elements of {@code a} in the same order.
4552      * If {@code a} is {@code null}, this method returns 0.
4553      *
4554      * @param a the array whose hash value to compute
4555      * @return a content-based hash code for {@code a}
4556      * @since 1.5
4557      */
4558     public static int hashCode(char a[]) {
4559         if (a == null)
4560             return 0;
4561 
4562         int result = 1;
4563         for (char element : a)
4564             result = 31 * result + element;
4565 
4566         return result;
4567     }
4568 
4569     /**
4570      * Returns a hash code based on the contents of the specified array.
4571      * For any two {@code byte} arrays {@code a} and {@code b}
4572      * such that {@code Arrays.equals(a, b)}, it is also the case that
4573      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4574      *
4575      * <p>The value returned by this method is the same value that would be
4576      * obtained by invoking the {@link List#hashCode() hashCode}
4577      * method on a {@link List} containing a sequence of {@link Byte}
4578      * instances representing the elements of {@code a} in the same order.
4579      * If {@code a} is {@code null}, this method returns 0.
4580      *
4581      * @param a the array whose hash value to compute
4582      * @return a content-based hash code for {@code a}
4583      * @since 1.5
4584      */
4585     public static int hashCode(byte a[]) {
4586         if (a == null)
4587             return 0;
4588 
4589         int result = 1;
4590         for (byte element : a)
4591             result = 31 * result + element;
4592 
4593         return result;
4594     }
4595 
4596     /**
4597      * Returns a hash code based on the contents of the specified array.
4598      * For any two {@code boolean} arrays {@code a} and {@code b}
4599      * such that {@code Arrays.equals(a, b)}, it is also the case that
4600      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4601      *
4602      * <p>The value returned by this method is the same value that would be
4603      * obtained by invoking the {@link List#hashCode() hashCode}
4604      * method on a {@link List} containing a sequence of {@link Boolean}
4605      * instances representing the elements of {@code a} in the same order.
4606      * If {@code a} is {@code null}, this method returns 0.
4607      *
4608      * @param a the array whose hash value to compute
4609      * @return a content-based hash code for {@code a}
4610      * @since 1.5
4611      */
4612     public static int hashCode(boolean a[]) {
4613         if (a == null)
4614             return 0;
4615 
4616         int result = 1;
4617         for (boolean element : a)
4618             result = 31 * result + (element ? 1231 : 1237);
4619 
4620         return result;
4621     }
4622 
4623     /**
4624      * Returns a hash code based on the contents of the specified array.
4625      * For any two {@code float} arrays {@code a} and {@code b}
4626      * such that {@code Arrays.equals(a, b)}, it is also the case that
4627      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4628      *
4629      * <p>The value returned by this method is the same value that would be
4630      * obtained by invoking the {@link List#hashCode() hashCode}
4631      * method on a {@link List} containing a sequence of {@link Float}
4632      * instances representing the elements of {@code a} in the same order.
4633      * If {@code a} is {@code null}, this method returns 0.
4634      *
4635      * @param a the array whose hash value to compute
4636      * @return a content-based hash code for {@code a}
4637      * @since 1.5
4638      */
4639     public static int hashCode(float a[]) {
4640         if (a == null)
4641             return 0;
4642 
4643         int result = 1;
4644         for (float element : a)
4645             result = 31 * result + Float.floatToIntBits(element);
4646 
4647         return result;
4648     }
4649 
4650     /**
4651      * Returns a hash code based on the contents of the specified array.
4652      * For any two {@code double} arrays {@code a} and {@code b}
4653      * such that {@code Arrays.equals(a, b)}, it is also the case that
4654      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4655      *
4656      * <p>The value returned by this method is the same value that would be
4657      * obtained by invoking the {@link List#hashCode() hashCode}
4658      * method on a {@link List} containing a sequence of {@link Double}
4659      * instances representing the elements of {@code a} in the same order.
4660      * If {@code a} is {@code null}, this method returns 0.
4661      *
4662      * @param a the array whose hash value to compute
4663      * @return a content-based hash code for {@code a}
4664      * @since 1.5
4665      */
4666     public static int hashCode(double a[]) {
4667         if (a == null)
4668             return 0;
4669 
4670         int result = 1;
4671         for (double element : a) {
4672             long bits = Double.doubleToLongBits(element);
4673             result = 31 * result + (int)(bits ^ (bits >>> 32));
4674         }
4675         return result;
4676     }
4677 
4678     /**
4679      * Returns a hash code based on the contents of the specified array.  If
4680      * the array contains other arrays as elements, the hash code is based on
4681      * their identities rather than their contents.  It is therefore
4682      * acceptable to invoke this method on an array that contains itself as an
4683      * element,  either directly or indirectly through one or more levels of
4684      * arrays.
4685      *
4686      * <p>For any two arrays {@code a} and {@code b} such that
4687      * {@code Arrays.equals(a, b)}, it is also the case that
4688      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4689      *
4690      * <p>The value returned by this method is equal to the value that would
4691      * be returned by {@code Arrays.asList(a).hashCode()}, unless {@code a}
4692      * is {@code null}, in which case {@code 0} is returned.
4693      *
4694      * @param a the array whose content-based hash code to compute
4695      * @return a content-based hash code for {@code a}
4696      * @see #deepHashCode(Object[])
4697      * @since 1.5
4698      */
4699     public static int hashCode(Object a[]) {
4700         if (a == null)
4701             return 0;
4702 
4703         int result = 1;
4704 
4705         for (Object element : a)
4706             result = 31 * result + (element == null ? 0 : element.hashCode());
4707 
4708         return result;
4709     }
4710 
4711     /**
4712      * Returns a hash code based on the "deep contents" of the specified
4713      * array.  If the array contains other arrays as elements, the
4714      * hash code is based on their contents and so on, ad infinitum.
4715      * It is therefore unacceptable to invoke this method on an array that
4716      * contains itself as an element, either directly or indirectly through
4717      * one or more levels of arrays.  The behavior of such an invocation is
4718      * undefined.
4719      *
4720      * <p>For any two arrays {@code a} and {@code b} such that
4721      * {@code Arrays.deepEquals(a, b)}, it is also the case that
4722      * {@code Arrays.deepHashCode(a) == Arrays.deepHashCode(b)}.
4723      *
4724      * <p>The computation of the value returned by this method is similar to
4725      * that of the value returned by {@link List#hashCode()} on a list
4726      * containing the same elements as {@code a} in the same order, with one
4727      * difference: If an element {@code e} of {@code a} is itself an array,
4728      * its hash code is computed not by calling {@code e.hashCode()}, but as
4729      * by calling the appropriate overloading of {@code Arrays.hashCode(e)}
4730      * if {@code e} is an array of a primitive type, or as by calling
4731      * {@code Arrays.deepHashCode(e)} recursively if {@code e} is an array
4732      * of a reference type.  If {@code a} is {@code null}, this method
4733      * returns 0.
4734      *
4735      * @param a the array whose deep-content-based hash code to compute
4736      * @return a deep-content-based hash code for {@code a}
4737      * @see #hashCode(Object[])
4738      * @since 1.5
4739      */
4740     public static int deepHashCode(Object a[]) {
4741         if (a == null)
4742             return 0;
4743 
4744         int result = 1;
4745 
4746         for (Object element : a) {
4747             final int elementHash;
4748             final Class<?> cl;
4749             if (element == null)
4750                 elementHash = 0;
4751             else if ((cl = element.getClass().getComponentType()) == null)
4752                 elementHash = element.hashCode();
4753             else if (element instanceof Object[])
4754                 elementHash = deepHashCode((Object[]) element);
4755             else
4756                 elementHash = primitiveArrayHashCode(element, cl);
4757 
4758             result = 31 * result + elementHash;
4759         }
4760 




2698         if (aLength != bLength)
2699             return false;
2700 
2701         return ArraysSupport.mismatch(a, aFromIndex,
2702                                       b, bFromIndex,
2703                                       aLength) < 0;
2704     }
2705 
2706     /**
2707      * Returns {@code true} if the two specified arrays of shorts are
2708      * <i>equal</i> to one another.  Two arrays are considered equal if both
2709      * arrays contain the same number of elements, and all corresponding pairs
2710      * of elements in the two arrays are equal.  In other words, two arrays
2711      * are equal if they contain the same elements in the same order.  Also,
2712      * two array references are considered equal if both are {@code null}.
2713      *
2714      * @param a one array to be tested for equality
2715      * @param a2 the other array to be tested for equality
2716      * @return {@code true} if the two arrays are equal
2717      */
2718     public static boolean equals(short[] a, short[] a2) {
2719         if (a==a2)
2720             return true;
2721         if (a==null || a2==null)
2722             return false;
2723 
2724         int length = a.length;
2725         if (a2.length != length)
2726             return false;
2727 
2728         return ArraysSupport.mismatch(a, a2, length) < 0;
2729     }
2730 
2731     /**
2732      * Returns true if the two specified arrays of shorts, over the specified
2733      * ranges, are <i>equal</i> to one another.
2734      *
2735      * <p>Two arrays are considered equal if the number of elements covered by
2736      * each range is the same, and all corresponding pairs of elements over the
2737      * specified ranges in the two arrays are equal.  In other words, two arrays
2738      * are equal if they contain, over the specified ranges, the same elements


4455             return a[i];
4456         }
4457     }
4458 
4459     /**
4460      * Returns a hash code based on the contents of the specified array.
4461      * For any two {@code long} arrays {@code a} and {@code b}
4462      * such that {@code Arrays.equals(a, b)}, it is also the case that
4463      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4464      *
4465      * <p>The value returned by this method is the same value that would be
4466      * obtained by invoking the {@link List#hashCode() hashCode}
4467      * method on a {@link List} containing a sequence of {@link Long}
4468      * instances representing the elements of {@code a} in the same order.
4469      * If {@code a} is {@code null}, this method returns 0.
4470      *
4471      * @param a the array whose hash value to compute
4472      * @return a content-based hash code for {@code a}
4473      * @since 1.5
4474      */
4475     public static int hashCode(long[] a) {
4476         if (a == null)
4477             return 0;
4478 
4479         int result = 1;
4480         for (long element : a) {
4481             int elementHash = (int)(element ^ (element >>> 32));
4482             result = 31 * result + elementHash;
4483         }
4484 
4485         return result;
4486     }
4487 
4488     /**
4489      * Returns a hash code based on the contents of the specified array.
4490      * For any two non-null {@code int} arrays {@code a} and {@code b}
4491      * such that {@code Arrays.equals(a, b)}, it is also the case that
4492      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4493      *
4494      * <p>The value returned by this method is the same value that would be
4495      * obtained by invoking the {@link List#hashCode() hashCode}
4496      * method on a {@link List} containing a sequence of {@link Integer}
4497      * instances representing the elements of {@code a} in the same order.
4498      * If {@code a} is {@code null}, this method returns 0.
4499      *
4500      * @param a the array whose hash value to compute
4501      * @return a content-based hash code for {@code a}
4502      * @since 1.5
4503      */
4504     public static int hashCode(int[] a) {
4505         if (a == null)
4506             return 0;
4507 
4508         int result = 1;
4509         for (int element : a)
4510             result = 31 * result + element;
4511 
4512         return result;
4513     }
4514 
4515     /**
4516      * Returns a hash code based on the contents of the specified array.
4517      * For any two {@code short} arrays {@code a} and {@code b}
4518      * such that {@code Arrays.equals(a, b)}, it is also the case that
4519      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4520      *
4521      * <p>The value returned by this method is the same value that would be
4522      * obtained by invoking the {@link List#hashCode() hashCode}
4523      * method on a {@link List} containing a sequence of {@link Short}
4524      * instances representing the elements of {@code a} in the same order.
4525      * If {@code a} is {@code null}, this method returns 0.
4526      *
4527      * @param a the array whose hash value to compute
4528      * @return a content-based hash code for {@code a}
4529      * @since 1.5
4530      */
4531     public static int hashCode(short[] a) {
4532         if (a == null)
4533             return 0;
4534 
4535         int result = 1;
4536         for (short element : a)
4537             result = 31 * result + element;
4538 
4539         return result;
4540     }
4541 
4542     /**
4543      * Returns a hash code based on the contents of the specified array.
4544      * For any two {@code char} arrays {@code a} and {@code b}
4545      * such that {@code Arrays.equals(a, b)}, it is also the case that
4546      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4547      *
4548      * <p>The value returned by this method is the same value that would be
4549      * obtained by invoking the {@link List#hashCode() hashCode}
4550      * method on a {@link List} containing a sequence of {@link Character}
4551      * instances representing the elements of {@code a} in the same order.
4552      * If {@code a} is {@code null}, this method returns 0.
4553      *
4554      * @param a the array whose hash value to compute
4555      * @return a content-based hash code for {@code a}
4556      * @since 1.5
4557      */
4558     public static int hashCode(char[] a) {
4559         if (a == null)
4560             return 0;
4561 
4562         int result = 1;
4563         for (char element : a)
4564             result = 31 * result + element;
4565 
4566         return result;
4567     }
4568 
4569     /**
4570      * Returns a hash code based on the contents of the specified array.
4571      * For any two {@code byte} arrays {@code a} and {@code b}
4572      * such that {@code Arrays.equals(a, b)}, it is also the case that
4573      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4574      *
4575      * <p>The value returned by this method is the same value that would be
4576      * obtained by invoking the {@link List#hashCode() hashCode}
4577      * method on a {@link List} containing a sequence of {@link Byte}
4578      * instances representing the elements of {@code a} in the same order.
4579      * If {@code a} is {@code null}, this method returns 0.
4580      *
4581      * @param a the array whose hash value to compute
4582      * @return a content-based hash code for {@code a}
4583      * @since 1.5
4584      */
4585     public static int hashCode(byte[] a) {
4586         if (a == null)
4587             return 0;
4588 
4589         int result = 1;
4590         for (byte element : a)
4591             result = 31 * result + element;
4592 
4593         return result;
4594     }
4595 
4596     /**
4597      * Returns a hash code based on the contents of the specified array.
4598      * For any two {@code boolean} arrays {@code a} and {@code b}
4599      * such that {@code Arrays.equals(a, b)}, it is also the case that
4600      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4601      *
4602      * <p>The value returned by this method is the same value that would be
4603      * obtained by invoking the {@link List#hashCode() hashCode}
4604      * method on a {@link List} containing a sequence of {@link Boolean}
4605      * instances representing the elements of {@code a} in the same order.
4606      * If {@code a} is {@code null}, this method returns 0.
4607      *
4608      * @param a the array whose hash value to compute
4609      * @return a content-based hash code for {@code a}
4610      * @since 1.5
4611      */
4612     public static int hashCode(boolean[] a) {
4613         if (a == null)
4614             return 0;
4615 
4616         int result = 1;
4617         for (boolean element : a)
4618             result = 31 * result + (element ? 1231 : 1237);
4619 
4620         return result;
4621     }
4622 
4623     /**
4624      * Returns a hash code based on the contents of the specified array.
4625      * For any two {@code float} arrays {@code a} and {@code b}
4626      * such that {@code Arrays.equals(a, b)}, it is also the case that
4627      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4628      *
4629      * <p>The value returned by this method is the same value that would be
4630      * obtained by invoking the {@link List#hashCode() hashCode}
4631      * method on a {@link List} containing a sequence of {@link Float}
4632      * instances representing the elements of {@code a} in the same order.
4633      * If {@code a} is {@code null}, this method returns 0.
4634      *
4635      * @param a the array whose hash value to compute
4636      * @return a content-based hash code for {@code a}
4637      * @since 1.5
4638      */
4639     public static int hashCode(float[] a) {
4640         if (a == null)
4641             return 0;
4642 
4643         int result = 1;
4644         for (float element : a)
4645             result = 31 * result + Float.floatToIntBits(element);
4646 
4647         return result;
4648     }
4649 
4650     /**
4651      * Returns a hash code based on the contents of the specified array.
4652      * For any two {@code double} arrays {@code a} and {@code b}
4653      * such that {@code Arrays.equals(a, b)}, it is also the case that
4654      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4655      *
4656      * <p>The value returned by this method is the same value that would be
4657      * obtained by invoking the {@link List#hashCode() hashCode}
4658      * method on a {@link List} containing a sequence of {@link Double}
4659      * instances representing the elements of {@code a} in the same order.
4660      * If {@code a} is {@code null}, this method returns 0.
4661      *
4662      * @param a the array whose hash value to compute
4663      * @return a content-based hash code for {@code a}
4664      * @since 1.5
4665      */
4666     public static int hashCode(double[] a) {
4667         if (a == null)
4668             return 0;
4669 
4670         int result = 1;
4671         for (double element : a) {
4672             long bits = Double.doubleToLongBits(element);
4673             result = 31 * result + (int)(bits ^ (bits >>> 32));
4674         }
4675         return result;
4676     }
4677 
4678     /**
4679      * Returns a hash code based on the contents of the specified array.  If
4680      * the array contains other arrays as elements, the hash code is based on
4681      * their identities rather than their contents.  It is therefore
4682      * acceptable to invoke this method on an array that contains itself as an
4683      * element,  either directly or indirectly through one or more levels of
4684      * arrays.
4685      *
4686      * <p>For any two arrays {@code a} and {@code b} such that
4687      * {@code Arrays.equals(a, b)}, it is also the case that
4688      * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4689      *
4690      * <p>The value returned by this method is equal to the value that would
4691      * be returned by {@code Arrays.asList(a).hashCode()}, unless {@code a}
4692      * is {@code null}, in which case {@code 0} is returned.
4693      *
4694      * @param a the array whose content-based hash code to compute
4695      * @return a content-based hash code for {@code a}
4696      * @see #deepHashCode(Object[])
4697      * @since 1.5
4698      */
4699     public static int hashCode(Object[] a) {
4700         if (a == null)
4701             return 0;
4702 
4703         int result = 1;
4704 
4705         for (Object element : a)
4706             result = 31 * result + (element == null ? 0 : element.hashCode());
4707 
4708         return result;
4709     }
4710 
4711     /**
4712      * Returns a hash code based on the "deep contents" of the specified
4713      * array.  If the array contains other arrays as elements, the
4714      * hash code is based on their contents and so on, ad infinitum.
4715      * It is therefore unacceptable to invoke this method on an array that
4716      * contains itself as an element, either directly or indirectly through
4717      * one or more levels of arrays.  The behavior of such an invocation is
4718      * undefined.
4719      *
4720      * <p>For any two arrays {@code a} and {@code b} such that
4721      * {@code Arrays.deepEquals(a, b)}, it is also the case that
4722      * {@code Arrays.deepHashCode(a) == Arrays.deepHashCode(b)}.
4723      *
4724      * <p>The computation of the value returned by this method is similar to
4725      * that of the value returned by {@link List#hashCode()} on a list
4726      * containing the same elements as {@code a} in the same order, with one
4727      * difference: If an element {@code e} of {@code a} is itself an array,
4728      * its hash code is computed not by calling {@code e.hashCode()}, but as
4729      * by calling the appropriate overloading of {@code Arrays.hashCode(e)}
4730      * if {@code e} is an array of a primitive type, or as by calling
4731      * {@code Arrays.deepHashCode(e)} recursively if {@code e} is an array
4732      * of a reference type.  If {@code a} is {@code null}, this method
4733      * returns 0.
4734      *
4735      * @param a the array whose deep-content-based hash code to compute
4736      * @return a deep-content-based hash code for {@code a}
4737      * @see #hashCode(Object[])
4738      * @since 1.5
4739      */
4740     public static int deepHashCode(Object[] a) {
4741         if (a == null)
4742             return 0;
4743 
4744         int result = 1;
4745 
4746         for (Object element : a) {
4747             final int elementHash;
4748             final Class<?> cl;
4749             if (element == null)
4750                 elementHash = 0;
4751             else if ((cl = element.getClass().getComponentType()) == null)
4752                 elementHash = element.hashCode();
4753             else if (element instanceof Object[])
4754                 elementHash = deepHashCode((Object[]) element);
4755             else
4756                 elementHash = primitiveArrayHashCode(element, cl);
4757 
4758             result = 31 * result + elementHash;
4759         }
4760 


< prev index next >