Java Streams将列表拆分为子列表,分别处理这些列表并将它们合并回来

Java Streams将列表拆分为子列表,分别处理这些列表并将它们合并回来,java,collections,lambda,java-8,java-stream,Java,Collections,Lambda,Java 8,Java Stream,我有一个用例,其中包含“Location”对象的列表需要根据locationName进行处理。 我用Java8流尝试过这个 private List<Payment> filterLocationsByName(List<Location> locationList) { return locationList.stream().filter(l -> l.getLocationName() .equalsIgnoreCase("som

我有一个用例,其中包含“Location”对象的列表需要根据locationName进行处理。 我用Java8流尝试过这个

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
专用列表过滤器LocationsByName(列表位置列表){
返回locationList.stream().filter(l->l.getLocationName()
.equalsIgnoreCase(“某些位置名称”))
.collect(Collectors.toList());
}
List originalLocationList=。。。。。
List someLocations=FilterLocationByName(originalLocationList);
//处理某些位置列表的逻辑
//对其他locationName执行相同的操作
//最后,您需要返回带有所做更改的原始列表
我的问题是某些位置列表没有原始列表的支持。我对某些位置元素所做的更改不会填充到原始列表中。
如何将此someLocations列表合并回原始列表,以便已处理的更改对原始列表生效?

流主要用于不可变的处理,因此通常不会更改原始流源(集合)。您可以尝试使用
forEach
,但您需要自己进行删除

另一个选项是使用from Collection接口(您只需要否定该条件):


这将更改列表。

如果可以的话,我会有点惊讶。
someLocations
没有原始列表的支持,但它包含对原始对象的引用,这些对象仍然从
originalLocationList
引用。如果您只想改变现有的
位置
对象,则无需将任何内容合并回来。问题是什么?@Krishan你的问题不清楚
filterLocationsByName
返回一个
列表
,您无法将其分配给
列表
。此外,
filterLocationsByName
没有将
列表
转换为
列表
。不确定此代码是否会编译。前提条件中已存在错误。请参阅:“对于返回的列表的类型、可变性、序列化性或线程安全性没有任何保证”。假设您可以修改返回的列表,这已经是一个错误。在这个特定实现的当前版本中,这恰好是可能的,但不能保证。
locationList.removeIf(
    l -> !l.getLocationName().equalsIgnoreCase("some_location_name")
);