Reputation: 17
I've been working on a drunk walker coding problem (custom user classes, etc.), and I'm going insane trying to fix this small issue.
I messed around with the code (to no avail), and so with no hope in sight, I decided to get an outside opinion.
The code I used for adding into the hashmap is like this:
if (hashMap.containsKey(key) == false) {
hashMap.put(key, 1);
}
else {
hashMap.put(key, value + 1);
}
In theory, this should be completely fine. If the key is not saved in map, it is added to the map with a value of 1. If the key is actually there in the map, then the value is incremented by one. The key is just an instance of a custom class that takes two integer variables. It is being constantly updated.
At the end of the program, if I display entries in the hashmap with values greater than 1, it should look something like this:
Visited Intersection [avenue=8, street=42] 3 times!
Visited Intersection [avenue=8, street=63] 2 times!
But when I observed what the hashmap looked like with each function call, it looked like this:
Hash Map: {Intersection [avenue=6, street=22]=1}
Hash Map: {Intersection [avenue=6, street=23]=1, Intersection
[avenue=6, street=23]=1}
Hash Map: {Intersection [avenue=6, street=22]=2, Intersection
[avenue=6, street=22]=1}
Hash Map: {Intersection [avenue=5, street=22]=2, Intersection
[avenue=5, street=22]=1, Intersection [avenue=5, street=22]=1}
Hash Map: {Intersection [avenue=6, street=22]=3, Intersection
[avenue=6, street=22]=1, Intersection [avenue=6, street=22]=1}
...
Every entry in the hashmap was being overwritten, and the end product was this:
Visited Intersection [avenue=8, street=20] 3 times!
Visited Intersection [avenue=8, street=20] 2 times!
Visited Intersection [avenue=8, street=20] 2 times!
Visited Intersection [avenue=8, street=20] 2 times!
...
Originally I thought the code for adding into the hashmap was incorrect, since every key was being overwritten and only the last updated one is displayed, but now I think it has to do with the actual updating of the key.
Penny for your thoughts? Sorry if it's a bit vague.
Upvotes: 0
Views: 710
Reputation: 116878
Every entry in the hashmap was being overwritten...
I suspect you don't quite understand how HashMap
works. HashMap
stores a reference to the key
not a copy. I suspect that you are overwriting the fields in the Intersection
after you have put it into the map. This is a very bad pattern and can result in some very strange results.
Couple of things to check.
new Intersection(avenue, street)
each time.Intersection
be final
. This is always a good pattern so you don't change the value of the key unintentionally. For sure if one or both of the fields are "identity" field, it should be final
.Intersection
object has appropriate hashcode()
and equals()
methods that correctly identifies each value. Otherwise each Intersection
will be stored in the map regardless if they have the same avenue
and street
values. See my answer here: https://stackoverflow.com/a/9739583/179850Maybe something like:
Intersection key = new Intersection(8, 42);
...
Integer count = hashMap.get(key);
if (count == null) {
hashMap.put(key, 1);
} else {
hashMap.put(key, value + 1);
}
...
public class Intersection {
// these fields can't be changed
private final int avenue;
private final int street;
...
Upvotes: 7