I want to compute differences between collections. When using CollectionUtils.subtract() for custom comparison I need to override the object’s equals() method. But what if I need to compare collections of objects of the same type but different comparison criterion? What about the Comparator interface, it seems perfectly suited here? AFAIK Comparator is mainly used for sorting. Isn’t there a method that uses Comparators for subtracting?
I want to compute differences between collections. When using CollectionUtils.subtract() for custom comparison I
Share
If you have an ArrayList, multiple removes can be more expensive than taking a copy.
Personally I would use a loop. Its likely to be shorter and clearer.
The reason an Iterator is used explicitly is to use the Iterator.remove() which avoids a ConcurrentModificationException. Another way to avoid it is to use a copy of the collection which might be preferred.
This doesn’t perform as well but may perform well enough.