I'm trying to write the get method for a hashmap of custom objects.
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();
}
}
I would expect that if d.getKey() and key print the same value and the same class type, then the if statement should come out true, and print the value which should be being returned. However, this is the result I get:
hashval: 5
d.getKey:12345 class: class java.lang.Integer
key:12345 class: class java.lang.Integer
The line inside the if statement does not show up. What silly problem am I overlooking? I think it might have something to do with templates, since key is the generic type K?