Arrays.hashCode(Object [])与Objects.hash(Object…)

JDK 1.5开始Arrays类提供了名为“ hashCode ”的重载static方法。 大多数重载方法都接受特定原始类型的数组,但是Arrays.hashCode(Object [])方法可用于计算引用类型数组的int哈希码。 自从JDK 1.7诞生以来Objects类提供了一种名为hash(Object…)的方法,该方法还为提供的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.hashCodeObjects.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.hashCodeObjects.hash(Object...)Objects.hashCode(Object)方法,该方法接受单个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...)对于引用类型IntegerString返回相同的计算哈希码,因为它们两者实际上都是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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要在Student类上实现Comparable接口,实现compareTo方法,按照学号大小进行比较: ```java public class Student implements Comparable<Student> { private int id; private String name; private int age; public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Student student = (Student) obj; return id == student.id && age == student.age && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(id, name, age); } @Override public int compareTo(Student o) { return Integer.compare(this.id, o.id); } } ``` 然后,我们需要在测试中创建一个学生对象数组,并使用Arrays.sort方法对其进行排序: ```java public class Test { public static void main(String[] args) { Student[] students = new Student[3]; students[0] = new Student(3, "Tom", 18); students[1] = new Student(1, "Lucy", 19); students[2] = new Student(2, "Jack", 20); Arrays.sort(students); for (Student student : students) { System.out.println(student); } } } ``` 输出结果: ``` Student{id=1, name='Lucy', age=19} Student{id=2, name='Jack', age=20} Student{id=3, name='Tom', age=18} ``` 可以看到,使用多态的方式调用了Arrays.sort方法,并且按照学号大小进行了排序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值