在一个lambda表达式中收集复杂的对象

时间:2019-05-06 14:35:10

标签: java lambda java-stream grouping

我有一个对象列表。首先,我需要按类型对其进行排序。 比按faceValue。最后,总结所有数量:

      class Coin{
            String type;
            BigInteger faceValue;
            BigInteger quantity;
...
       }

            List<Coin> coins = new ArrayList<>();
            coins.add(new Coin("USD", 1, 150));
            coins.add(new Coin("USD", 1, 6));
            coins.add(new Coin("USD", 1, 60));
            coins.add(new Coin("USD", 2, 100));
            coins.add(new Coin("USD", 2, 100));
            coins.add(new Coin("CAD", 1, 111));
            coins.add(new Coin("CAD", 1, 222));

结果列表必须仅包含3个新硬币对象:

Coin("USD", 1 , 216)
Coin("USD", 2 , 200)
Coin("CAD", 1 , 333)

这怎么只能用一个lambda表达式编写?

2 个答案:

答案 0 :(得分:2)

从Java 8开始:

按类型:

Map<String, List<Coin>> coinlistGrouped =
    coins.stream().collect(Collectors.groupingBy(w -> w.type));

按面值:

Map<String, List<Coin>> coinlistGrouped =
    coins.stream().collect(Collectors.groupingBy(w -> w.faceValue));
  • 您可以将它们都用于按类型分组,然后按面值分组

答案 1 :(得分:1)

您可以使用Collectors.toMap作为解决此问题:

public List<Coin> groupedCoins(List<Coin> coins) {
    return new ArrayList<>(
            coins.stream()
                    .collect(Collectors.toMap(
                            coin -> Arrays.asList(coin.getType(), coin.getFaceValue()), Function.identity(),
                            (coin1, coin2) -> {
                                BigInteger netQ = coin1.getQuantity().add(coin2.getQuantity());
                                return new Coin(coin1.getType(), coin1.getFaceValue(), netQ);
                            }))
                    .values());
}

或另一种复杂的班轮分组,总和为:

public List<Coin> groupedAndSummedCoins(List<Coin> coins) {
    return coins.stream()
            .collect(Collectors.groupingBy(Coin::getType,
                    Collectors.groupingBy(Coin::getFaceValue,
                            Collectors.reducing(BigInteger.ZERO, Coin::getQuantity, BigInteger::add))))
            .entrySet()
            .stream()
            .flatMap(e -> e.getValue().entrySet().stream()
                    .map(a -> new Coin(e.getKey(), a.getKey(), a.getValue())))
            .collect(Collectors.toList());
}