0

I have kind of a really strange problem. I have a simple Map called vectors where I store StrategyPairs as keys and Vectors as the values. When I print it, I get this result:

{net.softwarepage.facharbeit.normalgame.logic.StrategyPair@131e56d7=(1.0;2.0), net.softwarepage.facharbeit.normalgame.logic.StrategyPair@1e1bc985=(2.0;2.0), net.softwarepage.facharbeit.normalgame.logic.StrategyPair@d5415975=(0.0;2.0), net.softwarepage.facharbeit.normalgame.logic.StrategyPair@5bf8c6e7=(2.0;1.0)}

As you can see StrategyPair@131e56d7 is mapped to a Vector (1,2). Now I create a new StrategyPair. When I print it I get StrategyPair@131e56d7 (the same one as before). However, if I now call vectors.get(strategyPair) it returns null. This is somehow really strange as the key is the same (at least it prints the exact same thing out when I print it...)

The problem arises when I rename a strategy, e.g. I change the property name in the class "Strategy". Then suddenly the map which contains StrategyPairs (a wrapper class for two strategies) is messed up as I explained before...

EDIT: When I print the HashMap I still get the same result as above, but the following code:

for(StrategyPair pair : vectors.keySet()) {
            System.out.println(vectors.get(pair));
}

returns: null (2.0;2.0) null (2.0;1.0)

mathiasj
  • 125
  • 2
  • 12
  • 3
    What kind of map? How do you think the map should identify two equal keys? – Sotirios Delimanolis Mar 20 '15 at 23:44
  • @mathiasj Can you please implement `toString()` on your `StartegyPair` class? It is possible for two elements to have the same hash but not be equal. – k_g Mar 20 '15 at 23:51
  • Did you override equals and hashcode? – user253751 Mar 20 '15 at 23:53
  • It's a Hashmap. With toString overwritten, I get this result: `{OBEN + links=(1.0;2.0), unten + rechts=(2.0;2.0), OBEN + rechts=(0.0;2.0), unten + links=(2.0;1.0)} OBEN + links null` for this code: `System.out.println(vectors); StrategyPair pair = new StrategyPair(...); System.out.println(pair); System.out.println(vectors.get(pair));` Yes, equals and hashcode are both overwritten. – mathiasj Mar 20 '15 at 23:54
  • This is a not a HashMap. Please add your implementation of StrategyPair. – Sergey Shustikov Mar 20 '15 at 23:56
  • private final HashMap vectors = new HashMap<>(); – mathiasj Mar 20 '15 at 23:57
  • Please provide an [mcve](http://stackoverflow.com/help/mcve). – Sotirios Delimanolis Mar 20 '15 at 23:59
  • 1
    The hashcode of your keys should not change after you keep them in the map. If you change any of the properties of your object which effect the hashcode, then this behaviour can occur. Ideally keys should be immutable – RP- Mar 21 '15 at 00:00
  • This is my implementation of StrategyPair: http://pastebin.com/hEEWrzuD @RajendraGujja But I need to change the name of the strategy so the property of the key (and hence the hashcode) will change. What should I do then? But this makes some sense. – mathiasj Mar 21 '15 at 00:03
  • Add also Strategy implementation. You hashcode implementation related to this class. – Sergey Shustikov Mar 21 '15 at 00:10
  • Strategy: http://pastebin.com/hKeEcbwN – mathiasj Mar 21 '15 at 00:14

1 Answers1

0

As @Rajendra Gujja mentioned in a comment, the "hashcode of your keys should not change after you keep them in the map". This is very true; once I changed all the hashcodes to simply use a UUID instead of the name property which changes, the problem is solved. Thanks for all of your answers!

mathiasj
  • 125
  • 2
  • 12