I found an interesting bug in my code today. I’m storing an object in a cache, and using a couple of indexes to address it. One index is a normal
HashMap object, which is indexing the object based on its unique ID. The other index is applying a grouping – several objects with the same grouping could exists so here I use a
HashMap indexing a
HashSet object. When I need to update the object, I call
put on the ID index, which updates the entry. For the grouping index, I get the
HashSet and call
add. I noticed that the unique ID index was updating, but the grouping index wasn’t – I had of course failed to read the documentation for
Adds the specified element to this set if it is not already present.
This contrasts with the
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.
Now I should of read the documentation, but the reason for my mistake is that I knew that a
HashSet was actually backed by a
HashMap – as such I assumed
add would just call
put on the underlying
HashMap using the same object as a key and the value. Of course I am misusing a
Set here – a
Set is a collection of objects with no duplicates. When calling
equals is called, shows that the object is already there, and therefore doesn’t re-add it. Really I want the behaviour of a
Map with the simpler interface of a
Set, so I should just stop complaining and write my own wrapper class, shouldn’t I?