user2759511
user2759511

Reputation: 640

java HashCode cannot retrieve entries

I have a bug that I am not able to solve. My HashMap has some entries, but I am unable to retrieve them through the key. The keys do have hashCode and equals overridden, but as far as I know, they're working fine. They were implementing using eclipse's generator The following code proves the problem. The HashMap refuses to admit it contains a key that itself provided. Other methods like get do not work either.

HashMap<SimpleTurn, Turn> turns = node.turns;
System.out.println("verifying HashMap turns"); 
System.out.println(" class : "+turns.getClass().getName()+" "+turns.getClass().getCanonicalName());
for(SimpleTurn sp : turns.keySet()){
    System.out.println("Am I equal to myself : "+sp.equals(sp));
    System.out.println("Do I belong to my collection : "+turns.containsKey(sp));
}

Please note that I explicitly try to check my implementation of equals and it seems to work fine. The output from the above code is:

verifying HashMap turns
class : java.util.HashMap java.util.HashMap
Am I equal to myself : true
Do I belong to my collection : false
Am I equal to myself : true
Do I belong to my collection : false
Am I equal to myself : true
Do I belong to my collection : false

This behaviour is consistent throughout the code and What could be possible causing this behaviour? Any hints to what I should be looking for? Thank you very much

Upvotes: 0

Views: 70

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279870

The behavior you are describing can only happen if your hashCode returns something different than the value it used when first inserting the key. For example

public class Main {     
    public int count = 0;
    public static void main(String[] args) throws Exception {   //Read user input into the array
        HashSet<Main> set = new HashSet<>();
        Main main = new Main();
        main.count = 3;

        set.add(main);
        main.count = 2; // changes what hashCode() returns 

        System.out.println(set.contains(main)); // prints false
    }

    public int hashCode() {
        return count;
    }
}

A HashSet uses a HashMap in its underlying implementation.

The key is stored at a position defined by the original hashCode() value of 3. We modify the object and try to check if the Set contains it. The contains() method will call the hashCode() method again, which returns 2, and checks if there is an element defined (bucket) with that value. It doesn't find any and so returns false.

You must be modifying the object that is acting as the key after you add them to the Map.

Upvotes: 1

Related Questions