-->

Custom object as key in Collection

Posted by Admin on
What do you need to do to use a custom object as key in Collection classes like Map or Set?

Answer : If you are using any custom object in Map as key, you need to override equals() and hashCode() method, and make sure they follow there contract. On the other hand if you are storing a custom object in Sorted Collection e.g. SortedSet or SortedMap, you also need to make sure that your equals() method is consistent to compareTo() method, otherwise those collection will not follow there contacts e.g. Set may allow duplicates.

No comments:

Post a Comment