I have two list of different Objects.
class School { private String schoolName; private String location; private String pinCode; private String rating; } class World { private String schoolName; private String location; private String country; private String region; }
I want to remove the list of School objects from List of World objects based on schoolName
and location
. I cannot use equals
and hashCode
methods on those two fields as it is creating some other problem. Please help me how it can be done using streams.
Advertisement
Answer
You can use filter
:
worldList.stream() .filter(world -> schoolList.stream() .anyMatch(school -> world.getSchoolName().equals(school.getSchoolName()) && world.getLocation().equals(school.getLocation()) ) .collect(Collectors.toList());