Java 8 lambda 在许多情况下都非常有用,可以以紧凑的方式以 FP 方式实现代码。
但是在某些情况下,我们可能必须访问/改变外部状态,这不是按照 FP 实践的好做法。
(因为 Java 8 Functional 接口(interface)有严格的输入和输出签名,我们不能传递额外的参数)
例如:
class Country{
List<State> states;
}
class State{
BigInt population;
String capital;
}
class Main{
List<Country> countries;
//code to fill
}
假设用例是获取所有国家的所有首都和所有州的总人口
正常执行:
List<String> capitals = new ArrayList<>();
BigInt population = new BigInt(0);
for(Country country:countries){
for(State state:states){
capitals.add(state.capital);
population.add(state.population)
}
}
如何以更优化的方式使用 Java 8 Streams 实现同样的功能?
Stream<State> statesStream = countries.stream().flatMap(country->country.getStates());
capitals = statesStream.get().collect(toList());
population = statesStream.get().reduce((pop1,pop2) -> return pop1+pop2);
但是上面的实现不是很有效。任何使用 Java 8 Streams 操作多个集合的其他更好的方法
最佳答案
如果您想在一个管道中收集多个结果,您应该创建一个结果容器和一个自定义的Collector
。
class MyResult {
private BigInteger population = BigInteger.ZERO;
private List<String> capitals = new ArrayList<>();
public void accumulate(State state) {
population = population.add(state.population);
capitals.add(state.capital);
}
public MyResult merge(MyResult other) {
population = population.add(other.population);
capitals.addAll(other.capitals);
return this;
}
}
MyResult result = countries.stream()
.flatMap(c -> c.getStates().stream())
.collect(Collector.of(MyResult::new, MyResult::accumulate, MyResult::merge));
BigInteger population = result.population;
List<String> capitals = result.capitals;
或者像你一样直播两次。
关于Java 8 Streams 和 lambdas 保持严格的 FP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44697631/