Java: How do I perform list operations with different definitions of equals?
I have two lists of generic POJOs. I need to perform some set operations on the lists based on different ways of comparing the POJOs within the lists.
For example, if my POJO had the following structure:
public class GenericPojo {
private String id;
private String address;
private String city;
private String country;
private String extraDetails;
}
(with the appropriate getters and setters)
Given List1<GenericPojo>
and List2<GenericPojo>
, how would I find:
List1 - List2 (where the GenericPojo
classes are equal if just the IDs are equal)
Intersect of List1 and List2 (where id
, address
, city
, country
, but not extraDetails
of GenericPojo
are equal)
Would two different custom comparator classes be helpful here? Are there any libraries that handle these operations effectively or should I try implementing my own?