In the other post we saw how to find duplicates in the List of String & the same approach we can use for this problem also.
But there is a catch here...What is that?
In case of primitives & String, Java knows how to compare or how to establish the equivalency
but in case of custom object, Java needs help for this, we all know this.
So, here if the objects in your collections are Entity ones, then I expect that those will be
having hashCode() & equals() overriden. Then you will not see any surprise but if these
methods are not overriden then Java doesn't know how to process & every object will be
treated as unique one & you can see the unexpected result.
One can follow the below example & can try by commenting out the hashCode() & equals().
But there is a catch here...What is that?
In case of primitives & String, Java knows how to compare or how to establish the equivalency
but in case of custom object, Java needs help for this, we all know this.
So, here if the objects in your collections are Entity ones, then I expect that those will be
having hashCode() & equals() overriden. Then you will not see any surprise but if these
methods are not overriden then Java doesn't know how to process & every object will be
treated as unique one & you can see the unexpected result.
One can follow the below example & can try by commenting out the hashCode() & equals().