I'm maintaining a piece of software that is using an IdentityHashMap
to store a list of objects as the key
, but I noticed that everywhere in the code just populates the paired value
with null and nothing ever seems to reference the value
either. This feels wrong to me, I don't know why a map is being used when the value being stored is effectively ignored.
Based on my observations, the map key is being used to store/compare objects and the previous author is using contains
before populating the map to make sure no attempts to add duplicate objects are made.
I'm not familiar with all the different data structures in Java, but I'm guessing a collection
is better suited for this purpose. I came to HashSet
because the order doesn't matter and we don't want duplicates.
Where you guys come in is I'm wondering if my thought process is correct. Is there perhaps something I may not be considering as to why the original author went with IdentityHashMap
? Would HashSet
be the appropriate alternative where the value pairings are not required? Are there any performance considerations? (eg. is IdentityHashMap
actually faster than a HashSet
in this context?)
If HashSet
isn't the correct data structure, what is?