This is normal. Alternatively, you can use a HashSet .
EDIT:
You can compare objects in two ways:
By default, Object is compared by reference and implemented in the Object class.
If any class in the hierarchy of your object overrides this default implementation, then you make a comparison. If this happens, you must also override hashcode .
Given this, IF , you want to compare objects by reference, but there is a special equality implementation, then you should use IdentityHashMap ELSE use a HashSet .
If you want to keep your current implementation using HashMap , it is also great. HashSet is internally implemented using the HashMap. But instead of setting the value to 1, set it with null .
The question also arises about the correct data structure. You can use List instead of the hash structure. The type of data structure you should use is up to you. It depends on how many people think, how many objects you are going to invest in the collection, how much access will be to them, inserts, etc.
source share