pet*_*ter 2 java lambda functional-programming java-8 java-stream
我想知道是否有更好的方法来重写groupA方法与lambda作为链接操作的一部分?
public class Id {
private final int value;
public Id(int value) {
this.value = value;
}
public int value() {
return value;
}
}
public class Ids implements Iterable<Id> {
private final List<Id> ids;
private Ids(List<Id> ids) {
this.ids = ids;
}
public static Ids of(List<Id> ids) {
return new Ids(ids);
}
public Ids groupA() {
return Ids.of(ids.stream()
.filter(id -> id.value() > 5)
.collect(Collectors.toList()));
}
@Override
public Iterator<Id> iterator() {
return ids.iterator();
}
}
Run Code Online (Sandbox Code Playgroud)
基本上我想做点什么
ids.stream()
.filter(id -> id % 10 > 5)
.collect(Collectiors.toList())
.andThen(Ids::of);
Run Code Online (Sandbox Code Playgroud)
并且想知道这是否可能
Lou*_*man 10
当然.你可以直截了当地做
Ids.of(ids.stream()
.filter(id -> id % 10 > 5)
.collect(Collectors.toList()))
Run Code Online (Sandbox Code Playgroud)
或者你可以将它添加到收藏家:
ids.stream()
.filter(id -> id % 10 > 5)
.collect(Collectors.collectingAndThen(Collectors.toList(), Ids::of))
Run Code Online (Sandbox Code Playgroud)