I have a scenario in my code where I need to compare two lists and remove from the first list, objects which are present in the second list. Akin to how the "removeAll" object works for List. Since my list is created on a custom object, the removeAll method won't work for me.
I have tried various methods to make this work:
- implemented equals() and hashCode for the custom object comprising the list
- implemented the Comparable Interface for the custom object
- implemented the Comparator Interface for the custom object
I've even tried using the Apache Common's CollectionUtils and ListUtils methods (subtract, intersect, removeAll). None seem to work.
I understand I will perhaps need to write some custom removal code. But not sure how to go about doing that. Any pointers helping me move in the right direction will be really appreciated.
equals
andhashCode
methods. You probably tried these but wrote them incorrectly. This is the correct approach and will work withremoveAll()
if done right. – Stephen