commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ludwig Magnusson <>
Subject [Collections] retainAll and removeAll with different equal-methods
Date Sun, 13 May 2012 11:02:12 GMT
I have two lists and I want to use the methods ListUtils.retainAll and ListUtils.removeAll
on them. However, I want to do this several times with comparison methods that are different
from the defaul equals-method. I.e I would like to compare the elements in the two lists from
several different prespectives. 

Consider a class Person that has the attributes firstName and lastName. I could define the
equals method to say that two persons equals if they have the same first name. I could also
say that they equals if they have the same full name. But what if I want to alternate?

So basically what I want to do is this:
List<Person> firstList; 
List<Person> secondList;
List<Person> firstNamesThatOccurInBothLists = ListUtils.retainAll(firstList, secondList,
[Third equals-comparator argument]);
List<Person> fullNamesThatOccurInBothLists = ListUtils.retainAll(firstList, secondList,
[Third equals-comparator argument]);

Can something of this effect be done with commons collections in any way?
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message