Java 8 Streams& lambdas保持严格的FP

时间:2017-06-22 11:08:23

标签: java lambda functional-programming java-8 java-stream

Java 8 lambdas在许多情况下非常有用,可以以紧凑的方式以FP方式实现代码     但是在某些情况下我们可能不得不访问/改变外部状态,这不是FP实践中的好习惯 (因为Java 8 Functional接口具有严格的输入和输出签名,我们无法传递额外的参数)

例如:

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操作多个集合的其他更好方法

3 个答案:

答案 0 :(得分:6)

如果您想在一个管道中收集多个结果,您应该创建一个结果容器和一个自定义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;

或者像你一样流两次。

答案 1 :(得分:1)

您只能使用一次流,因此您需要创建一个可以减少的聚合:

public class CapitalsAndPopulation {
  private List<String> capitals;
  private BigInt population;

  // constructors and getters omitted for conciseness

  public CapitalsAndPopulation merge(CapitalsAndPopulation other) {
    return new CapitalsAndPopulation(
      Lists.concat(this.capitals, other.capitals),
      this.population + other.population);
  }
}

然后你生成管道:

countries.stream()
  .flatMap(country->
    country.getStates()
      .stream())
  .map(state -> new CapitalsAndPopulation(Collections.singletonList(state.getCapital()), state.population))
  .reduce(CapitalsAndPopulation::merge);

这看起来很难看的原因是你没有很好的语法来构建元组或地图等结构,所以你需要创建类来使管道看起来很好......

答案 2 :(得分:1)

试试这个。

class Pair<T, U> {
    T first;
    U second;

    Pair(T first, U second) {
        this.first = first;
        this.second = second;
    }
}

Pair<List<String>, BigInteger> result = countries.stream()
    .flatMap(country -> country.states.stream())
    .collect(() -> new Pair<>(
            new ArrayList<>(),
            BigInteger.ZERO
        ),
        (acc, state) -> {
            acc.first.add(state.capital);
            acc.second = acc.second.add(state.population);
        },
        (a, b) -> {
            a.first.addAll(b.first);
            a.second = a.second.add(b.second);
        });

您可以使用AbstractMap.Entry<K, V>代替Pair<T, U>

Entry<List<String>, BigInteger> result = countries.stream()
    .flatMap(country -> country.states.stream())
    .collect(() -> new AbstractMap.SimpleEntry<>(
            new ArrayList<>(),
            BigInteger.ZERO
        ),
        (acc, state) -> {
            acc.getKey().add(state.capital);
            acc.setValue(acc.getValue().add(state.population));
        },
        (a, b) -> {
            a.getKey().addAll(b.getKey());
            a.setValue(a.getValue().add(b.getValue()));
        });