I have a class called Person –
public class Person implements Nameable {
private String name;
public String getName(){
return name;
}
}
Now I have two lists –
List<Person> persons = // some persons
List<Person> subsetOfPersons = // some duplicate persons, but different objects and don't share the same identity
Now I would like to filter the persons which are not present in the subsetOfPersons, equality criteria is name property and Person doesn’t have equals.
How can I do this?
I’m sure there’s a simpler way… the below would transform person to name for the sake of comparison. For the
subsetOfPersons, we actually create a list of names directly, since that’s all we really need from them. For thepersons, we keep the transformation limited to the context of the comparison.Edit: Thought you might appreciate a JUnit:
Edit again: Missed the “not” requirement the first time. Easy fix–with predicates, you can just wrap with
Predicates.not(..)!