wordpress移动底部菜单插件,郑州企业网站快速优化价格,佛山网络,泸州做网站公司从JDK 1.5开始 #xff0c; Arrays类提供了名为“ hashCode ”的重载static方法。 大多数重载方法都接受特定原始类型的数组#xff0c;但是Arrays.hashCode#xff08;Object []#xff09;方法可用于计算引用类型数组的int哈希码。 自从JDK 1.7诞生以来 #xff0c; Obj… 从JDK 1.5开始 Arrays类提供了名为“ hashCode ”的重载static方法。 大多数重载方法都接受特定原始类型的数组但是Arrays.hashCodeObject []方法可用于计算引用类型数组的int哈希码。 自从JDK 1.7诞生以来 Objects类提供了一种名为hashObject…的方法该方法还为提供的Java对象数组表示Java varargs的省略号 [ ... ] 作为数组处理 返回int哈希码。 接受一个数组 。 这篇文章提供了Arrays.hashCode(Object)和Objects.hash(Object...)之间的简要比较。 我们可以查看OpenJDK中的代码以了解OpenJDK如何实现此处比较的两种方法。 事实证明 Arrays.hashCode(Object[])和Objects.hash(Object...)行为完全相同因为Objects.hash(Object...)完全委托给Arrays.hashCode(Object[]) 。 这是从OpenJDK Objects.java类提取的下一个代码清单中显示的。 public static int hash(Object... values) {return Arrays.hashCode(values);
} 因此事实证明这些方法实际上是相同的因此选择哪种方法主要取决于口味。 鉴于无论如何都会调用Arrays方法可能会吸引一些人直接使用它。 其他人可能更喜欢在将已知的Java数组构造传递给Arrays方法时使用Objects方法而在以逗号分隔的组合形式传递值而无需显式数组语法的情况下使用Objects方法例如例如实现自定义类的hashCode()方法并将该类的任意类型的属性传递给哈希代码计算的情况。 当使用相同类型的原语数组时最好为该特定原语使用适当版本的Arrays.hashCode 。 下一个代码清单可在GitHub上找到中显示的简单类演示了Arrays.hashCode和Objects.hash(Object...)方法的重载版本之间的输出差异和相似之处。 package dustin.examples.hashcodes;import java.util.Arrays;
import java.util.Objects;import static java.lang.System.out;/*** Demonstration that displays output to standard output with* hash codes generated for the same underlying array data by* both {code Arrays.hashCode(Object[])} and by* {code Objects.hash(Object...)}.*/
public class HashesComparedDemo
{public static void main(final String[] arguments){final int[] integers ArraysCreator.createArrayOfInts();out.println(Arrays.hashCode(Object[]) for int[]: Arrays.hashCode(integers));out.println(Objects.hash(Object...) for int[]: Objects.hash(integers));out.println(Objects.hashCode(Object) for int[]: Objects.hashCode(integers));final Integer[] refIntegers ArraysCreator.createArrayOfIntegers();out.println(Arrays.hashCode(Object[]) for Integer[]: Arrays.hashCode(refIntegers));out.println(Objects.hash(Object...) for Integer[]: Objects.hash(refIntegers));out.println(Objects.hashCode(Object) for Integer[]: Objects.hashCode(refIntegers));final String[] strings ArraysCreator.createArrayOfStrings();out.println(Arrays.hashCode(Object[]) for String[]: Arrays.hashCode(strings));out.println(Objects.hash(Object...) for String[]: Objects.hash(strings));out.println(Objects.hashCode(Object) for String[]: Objects.hashCode(strings));}
} 上面显示的代码将三个公共数据集原始int值数组参考Integer值数组和String值数组传递给Arrays.hashCode Objects.hash(Object...)和Objects.hashCodeObject方法该方法接受单个Object 整个数组符合条件。 然后简单示例将每种方法为每个数据集生成的各个哈希码值写入标准输出。 接下来显示运行该代码的结果。 Arrays.hashCode(Object[]) for int[]: 1722319241
Objects.hash(Object...) for int[]: 356573628
Objects.hashCode(Object) for int[]: 356573597
Arrays.hashCode(Object[]) for Integer[]: 1722319241
Objects.hash(Object...) for Integer[]: 1722319241
Objects.hashCode(Object) for Integer[]: 1735600054
Arrays.hashCode(Object[]) for String[]: 448603921
Objects.hash(Object...) for String[]: 448603921
Objects.hashCode(Object) for String[]: 21685669 如我们所料 Arrays.hashCode(Object[])和Objects.hash(Object...)对于引用类型Integer和String返回相同的计算哈希码因为它们两者实际上都是Arrays.hashCode(Object[]) 。 原始int值数组从Arrays.hashCode(int[])得出的结果与从Objects.hash(Object...)得出的结果不同这当然是因为原始数组被传递给重载的Arrays.hashCode(int[])方法专门针对该原始数据类型而不是Arrays.hashCode(Object[]) 。 翻译自: https://www.javacodegeeks.com/2018/09/arrays-hashcodeobject-versus-objects-hashobject.html