Java8 嵌套流回写 setter
Java8 Nested Streams write back with setter
我正在尝试遍历两个列表,过滤嵌套列表并将结果写回具有 java8 功能的主对象。
locations.forEach(location -> location.getSubList().stream()
.filter(this::correctTestDataValue)
.collect(Collectors.toList()));
所以现在location里面的子列表没有改变,就是
很明显,因为 stream 和 collect 确实创建了一个新列表,它
不会写回位置对象。
所以我的问题是,是否有办法调用 setSubList(...) 方法
位置对象并将新列表写入其中。
感谢
我会使用 for 循环:
for (Location location : locations) {
List<?> newList = location.getSubList().stream()
.filter(this::correctTestDataValue)
.collect(Collectors.toList());
location.setSubList(newList);
}
或者如果你可以就地删除:
for (Location location : locations) {
location.getSubList().removeIf(x -> !correctTestDataValue(x));
}
哪个可以作为流工作:
locations.stream()
.map(Location::getSublist)
.forEach(list -> list.removeIf(x -> !correctTestDataValue(x)));
我正在尝试遍历两个列表,过滤嵌套列表并将结果写回具有 java8 功能的主对象。
locations.forEach(location -> location.getSubList().stream()
.filter(this::correctTestDataValue)
.collect(Collectors.toList()));
所以现在location里面的子列表没有改变,就是 很明显,因为 stream 和 collect 确实创建了一个新列表,它 不会写回位置对象。 所以我的问题是,是否有办法调用 setSubList(...) 方法 位置对象并将新列表写入其中。
感谢
我会使用 for 循环:
for (Location location : locations) {
List<?> newList = location.getSubList().stream()
.filter(this::correctTestDataValue)
.collect(Collectors.toList());
location.setSubList(newList);
}
或者如果你可以就地删除:
for (Location location : locations) {
location.getSubList().removeIf(x -> !correctTestDataValue(x));
}
哪个可以作为流工作:
locations.stream()
.map(Location::getSublist)
.forEach(list -> list.removeIf(x -> !correctTestDataValue(x)));