はじめに
この実験では、Java の hashCode() メソッドについて学びます。ハッシュ化とは、与えられたキーに対して一意の値を生成するために使用される技術です。ハッシュ化はハッシュテーブルを実装するために使用され、これらのデータ構造はデータの検索を高速かつ効率的に行う方法を提供します。hashCode() メソッドは、与えられたキーに対して整数値を返します。hashCode() メソッドの使い方と、強力な hashCode() メソッドを書く重要性について学びます。
この実験では、Java の hashCode() メソッドについて学びます。ハッシュ化とは、与えられたキーに対して一意の値を生成するために使用される技術です。ハッシュ化はハッシュテーブルを実装するために使用され、これらのデータ構造はデータの検索を高速かつ効率的に行う方法を提供します。hashCode() メソッドは、与えられたキーに対して整数値を返します。hashCode() メソッドの使い方と、強力な hashCode() メソッドを書く重要性について学びます。
まず、名前、登録番号、GPA などのフィールドを持つ Student クラスを作成しましょう。
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() メソッドは、2 つのオブジェクトが等しいかどうかを確認するために使用されます。この場合、私たちは 2 つの Student オブジェクトを、それらの名前、登録番号、および GPA に基づいて比較します。
@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() メソッドを実装します。
@Override
public int hashCode() {
return (int) this.name.charAt(0) - 64;
}
これで、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() メソッドを書きましょう。
@Override
public int hashCode() {
return ((int) this.name.charAt(0) - 64) * this.regNo * (int) this.gpa;
}
String クラスの組み込みの hashCode() メソッドを使って、Student クラスのハッシュコードを生成することもできます。
@Override
public int hashCode() {
return Objects.hash(name, regNo, gpa);
}
次に、equals() メソッドを使って 2 つの 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));
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()); // 出力:2
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()); // 出力:2
コードを実行するには、まず cd コマンドを使ってコードが保存されているディレクトリに移動します。その後、javac コマンドを使ってコードをコンパイルし、java コマンドを使ってコードを実行します。
cd ~/project
javac HashCodeDemo.java
java HashCodeDemo
この実験では、Java における hashCode() メソッドについて学びました。このメソッドは、与えられたキーに対して一意の値を生成するために使用されます。Student クラスに対して hashCode() と equals() メソッドを実装し、強力な hashCode() メソッドを書く方法を学びました。また、HashSet と HashMap において hashCode() と equals() メソッドをどのように使用するかも学びました。