カスタム オブジェクトのハッシュマップの get メソッドを作成しようとしています。
public V get(K key){
int hashval = (int)(Math.abs(key.hashCode()) % this.capacity);
for(Data<K, V> d : hashmap.get(hashval) ){
System.out.println("hashval: " + hashval);
System.out.println("d.getKey:" + d.getKey() + " class: " + d.getKey().getClass());
System.out.println("key:" + key + " class: " + key.getClass());
if (d.getKey() == key){
System.out.println("d.getValue: " + d.getValue());
return d.getValue();
}
}
d.getKey() と key が同じ値と同じクラス タイプを出力する場合、if ステートメントは true になり、返される値を出力するはずです。しかし、これは私が得る結果です:
hashval: 5
d.getKey:12345 class: class java.lang.Integer
key:12345 class: class java.lang.Integer
if ステートメント内の行は表示されません。私が見落としているばかげた問題は何ですか?キーはジェネリック型 K であるため、テンプレートと関係があるのではないかと思います。