Java 哈希与 HashCode

JavaJavaBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,我们将学习 Java 中的 hashCode() 方法。哈希(Hashing)是一种用于为给定键生成唯一值的技术。哈希用于实现哈希表(Hash Tables),这些数据结构提供了一种更快、更高效的方式来查找数据。hashCode() 方法为给定键返回一个整数值。我们将学习如何使用 hashCode() 方法,以及编写一个强健的 hashCode() 方法的重要性。

创建 Student 类

首先,让我们创建一个 Student 类,包含诸如姓名、注册号和 GPA 等字段。

class Student {
  String name;
  int regNo;
  double gpa;

  Student(String name, int regNo, double gpa) {
    this.name = name;
    this.regNo = regNo;
    this.gpa = gpa;
  }

}

使用 equals() 方法

equals() 方法用于检查两个对象是否相等。在本例中,我们将根据姓名、注册号和 GPA 来比较两个 Student 对象。

@Override
public boolean equals(Object obj) {
  if (this == obj) {
    return true;
  }
  if (obj == null || getClass() != obj.getClass()) {
    return false;
  }
  Student student = (Student) obj;
  return regNo == student.regNo &&
         Double.compare(student.gpa, gpa) == 0 &&
         Objects.equals(name, student.name);
}

实现 hashCode() 方法

hashCode() 方法为给定键返回一个整数值。我们将实现一个简单的 hashCode() 方法,它仅根据学生姓名的第一个字母返回一个整数。

@Override
public int hashCode() {
  return (int) this.name.charAt(0) - 64;
}

使用 hashCode() 方法

现在我们可以使用 hashCode() 方法为 Student 对象生成哈希码。

Student s1 = new Student("Alice", 1, 3.7);
Student s2 = new Student("Bob", 2, 3.9);

System.out.println("s1's hash code: " + s1.hashCode());
System.out.println("s2's hash code: " + s2.hashCode());

实现一个更强的 hashCode() 方法

之前的 hashCode() 方法并不够强,因为姓名首字母相同的学生会被分配相同的哈希值。让我们使用类的其他字段来编写一个更强的 hashCode() 方法。

@Override
public int hashCode() {
  return ((int) this.name.charAt(0) - 64) * this.regNo * (int) this.gpa;
}

使用内置的 hashCode() 方法

我们也可以使用 String 类的内置 hashCode() 方法来为 Student 类生成哈希码。

@Override
public int hashCode() {
  return Objects.hash(name, regNo, gpa);
}

测试相等性

现在,让我们使用 equals() 方法测试两个 Student 对象的相等性。

Student s1 = new Student("Alice", 1, 3.7);
Student s2 = new Student("Bob", 2, 3.9);
Student s3 = new Student("Alice", 1, 3.7);

System.out.println("s1 equals s2: " + s1.equals(s2));
System.out.println("s1 equals s3: " + s1.equals(s3));

为 Set 实现 hashCode()

我们可以在 HashSet 中使用 Student 类。为了让 HashSet 正常工作,我们需要同时重写 equals()hashCode() 方法。

Set<Student> studentSet = new HashSet<>();
studentSet.add(new Student("Alice", 1, 3.7));
studentSet.add(new Student("Bob", 2, 3.9));
studentSet.add(new Student("Alice", 1, 3.7));

System.out.println(studentSet.size()); // Output: 2

为 Map 实现 hashCode()

我们也可以在 HashMap 中使用 Student 类。为了让 HashMap 正常工作,我们需要同时重写 equals()hashCode() 方法。

Map<Student, String> studentMap = new HashMap<>();
studentMap.put(new Student("Alice", 1, 3.7), "Good");
studentMap.put(new Student("Bob", 2, 3.9), "Excellent");
studentMap.put(new Student("Alice", 1, 3.7), "Very Good");

System.out.println(studentMap.size()); // Output: 2

运行代码

要运行代码,首先使用 cd 命令导航到代码保存的目录。然后使用 javac 命令编译代码,并使用 java 命令运行代码。

cd ~/project
javac HashCodeDemo.java
java HashCodeDemo

总结

在本实验中,我们学习了 Java 中的 hashCode() 方法,它用于为给定键生成唯一值。我们为 Student 类实现了 hashCode()equals() 方法,并学习了如何编写一个健壮的 hashCode() 方法。我们还学习了如何在 HashSetHashMap 中使用 hashCode()equals() 方法。

您可能感兴趣的其他 Java 教程