Both of the operations you are trying to perform are functional, although Java does not support them very well, and you are likely to write them in a completely different way. You may need to rethink what you are trying to achieve to fit java.
What you do is perform an operation on a projection of a data type (i.e. for a subset of fields)
The operations you use are also Set operations, not List operations. for example, you cannot cross two lists (or at least you must determine what this means). Delete may not perform exactly as you expect from a list.
Imagine you have a method that returns a pojos collection with only the fields you specify. I wrote a library to do this efficiently with a dynamically generated class in the past, to look at functional Java or similar.
public static <Pojo, Pojo2> Set<Pojo2> project(Collection<Pojo> collection, String... fieldsToRetain);
List1 - List2 (where the GenericPojo classes are equal, if only the identifiers are equal)
Set<PojoWithId> setOfIds = project(list1, "id") setOfIds.retainAll(project(list2, "id"));
Intersection of List1 and List2 (where id, address, city, country, but not extraDetails of GenericPojo are equal)
Set<PojoWithThreeFields> intersection = project(list1, "id", "address", "city", "country"); intersection.retainAll(project(list2, "id", "address", "city", "country"));
Peter Lawrey
source share