【问题标题】:groupingBy in Streams with CollectingAndThen method -> how to get rid of Optional then use Max on the map :) [duplicate]使用 CollectingAndThen 方法在 Streams 中的 groupingBy -> 如何摆脱 Optional 然后在地图上使用 Max :) [重复]
【发布时间】:2020-10-06 15:15:45
【问题描述】:

似乎我不太了解 groupingBy 和收集器和排序的概念。

任务:使用 Streams 对按类别分组的订单数量求和。然后获取最大数量的类别并将其打印在图片上作为具有最高值的单个地图行

3 类(2 条记录:产品和订单 + 主要)。在 Main 中有一个 List.of 新订单

类产品:

public record Product(String name, BigDecimal price, String category)

班级顺序:

public record Order(Product product, int quantity, BigDecimal discount)

public BigDecimal priceWithDiscount(){
    return product.price().multiply(BigDecimal.ONE.subtract(discount));
}

类主

       List<Order> orders = List.of(
            new Order(new Product("chleb", new BigDecimal(5), "A"),10, new BigDecimal("0.1")),
            new Order(new Product("maslo", new BigDecimal(6), "A"),5, new BigDecimal("0.2")),
            new Order(new Product("szynka", new BigDecimal(25), "B"),10, new BigDecimal("0")),
            new Order(new Product("kielbasa", new BigDecimal(16),"C"),5, new BigDecimal("0")),
            new Order(new Product("woda", new BigDecimal(3),"B"),15, new BigDecimal("0.1")),
            new Order(new Product("ocet", new BigDecimal(3),"A"),8, new BigDecimal("0.3")),
            new Order(new Product("margaryna", new BigDecimal(4),"B"),12, new BigDecimal("0.5")),
            new Order(new Product("maslo", new BigDecimal(8),"C"),5, new BigDecimal("0.2"))
            )

下面是我的分组实现:

        Map<String, Optional<Integer>> summedQuantitiesPerCategory = orders //no 1.
            .stream()
            .collect(Collectors.groupingBy(p -> p.product().category(),
                    Collectors.collectingAndThen(
                            Collectors.mapping(p -> p.quantity(), Collectors.toList()),
                            quantity -> quantity.stream().reduce((x, y) -> x + y) 
                    )));


        summedQuantitiesPerCategory
            .entrySet()
            .stream()
            .sorted(Comparator.comparing(p -> p.getValue())) // no2.
            .limit(1);

问题:

  1. 如何摆脱这个 Optional 并在映射中只看到 Integer 作为值。那么我猜它很容易排序
  2. 如何使用 Sorted 方法或更简单的方法通过 Value 对地图进行排序,例如最大?

【问题讨论】:

  • 你可以使用Collectors.toMap也可以使用orders.stream().collect(Collectors.toMap(p -&gt; p.product().category(), p -&gt; p.quantity(), (a,b) -&gt; a + b));
  • “或更简单的东西,例如最大”如何咨询the documentation,一生至少一次?

标签: java sorting stream java-stream groupingby


【解决方案1】:

您正在使用reduce 的一个参数版本,它接受BinaryOperator。您可以将标识值与BinaryOperator 一起传递。

quantity -> quantity.stream().reduce(0, (x, y) -> x + y)

或

quantity -> quantity.stream().reduce(0, Integer::sum)

既然你只是想求和数量,你可以使用Collectors.summingInt

...
.collect(Collectors.groupingBy(p -> p.product().category(),
                Collectors.summingInt(p -> p.quantity())));

【讨论】:

  • 所有解决方案都有效,非常感谢,现在我不仅有代码,我的整个概念对我来说更清楚了。再次感谢!
猜你喜欢
  • 2022-11-22
  • 2018-10-19
  • 1970-01-01
  • 1970-01-01
  • 2017-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多