Reputation: 641
I have a use case, in which a list containing 'Location' objects needs to be processed based on locationName. I tried this with Java 8 streams,
private List<Payment> filterLocationsByName(List<Location> locationList) {
return locationList.stream().filter(l -> l.getLocationName()
.equalsIgnoreCase("some_location_name"))
.collect(Collectors.toList());
}
List<Location> originalLocationList = .....
List<Location> someLocations = filterLocationsByName(originalLocationList);
//logic to process someLocations list
// do the same for another locationName
//at the end need to return the originalList with the changes made
My problem is someLocations list is not backed by the original list. Changes I do for someLocations elements are not populated in the original list. How can I merge this someLocations list back to the original list so that processed changes are in effect on the original list?
Upvotes: 0
Views: 247
Reputation: 27476
Streams are mostly for immutable processing, so you normally don't change the original stream source (collection). You could try with forEach
but you would need to do removal yourself.
Another option is to use removeIf
from Collection interface (you just need to negate the condition):
locationList.removeIf(
l -> !l.getLocationName().equalsIgnoreCase("some_location_name")
);
This will change the list in place.
Upvotes: 1