您可以使用
Collectors.toMapas 解决:
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());}


