使用 reduce 方法在 Stream 中添加 BigDecimal。

huangapple go评论79阅读模式
英文:

Adding BigDecimal in Stream using reduce method

问题

任务:计算列表<OrderPosition> orderPositions 中属于类别 A 的产品的总价格。

目标:练习使用 ".reduce" 方法。

有 3 个类:

  • Category 枚举 (A、B、C、D)
  • Product 类 (String name, BigDecimal price, BigDecimal discount, Category category)
  • OrderPosition 类 - 产品的数量

问题:App1 类中的流操作。

以下是代码:

public class Product {

    private String name;
    private BigDecimal price;
    private BigDecimal discount;
    private Category category;

    public Product(String name, BigDecimal price, BigDecimal discount, Category category) {
        this.name = name;
        this.price = price;
        this.discount = discount;
        this.category = category;
    }
}

public class OrderPosition {

    private Product product;
    private int quantity;

    public OrderPosition() {
    }

    public OrderPosition(Product product, int quantity) {
        this.product = product;
        this.quantity = quantity;
    }
}

public enum Category {
    A, B, C, D
}

public class App1 {
    public static void main(String[] args) {

        List&lt;OrderPosition&gt; orderPositions = List.of(
            new OrderPosition(new Product("Szynka", new BigDecimal("25"), BigDecimal.ZERO, Category.A), 10),
            new OrderPosition(new Product("Ser", new BigDecimal("25.5"), BigDecimal.ZERO, Category.B), 20),
            new OrderPosition(new Product("Chleb", new BigDecimal("15.5"), BigDecimal.ZERO, Category.C), 10),
            new OrderPosition(new Product("Nabial", new BigDecimal("8"), BigDecimal.ZERO, Category.D), 2),
            new OrderPosition(new Product("Kielbasa", new BigDecimal("15"), BigDecimal.ZERO, Category.A), 10),
            new OrderPosition(new Product("Jajko", new BigDecimal("5"), BigDecimal.ZERO, Category.B), 5)
        );

        Optional&lt;BigDecimal&gt; op = orderPositions
            .stream()
            .filter(orderPosition -&gt; orderPosition.getProduct().getCategory().equals(Category.A))
            .map(orderPosition -&gt; orderPosition.getProduct().getPrice().multiply(BigDecimal.valueOf(orderPosition.getQuantity())))
            .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}

提前感谢您!

英文:

Task: count the sum of products prices from the List<OrderPosition> orderPositions that have category A.

Goal: to practise ".reduce" method

There are 3 classes:
x Category enum (A, B, C, D),
x Product (String name, BigDecimal price, BigDecimal discount, Category category)
x OrderPositin - quantity of the product

Problem: class App1 stream op

Code below

public class Product {
private String name;
private BigDecimal price;
private BigDecimal discount;
private Category category;
public Product(String name, BigDecimal price, BigDecimal discount, Category category) {
this.name = name;
this.price = price;
this.discount = discount;
this.category = category;
}
public class OrderPosition {
private Product product;
private int quantity;
public OrderPosition() {
}
public OrderPosition(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public enum Category {
A, B, C, D
}
public class App1 {
public static void main(String[] args) {
List&lt;OrderPosition&gt; orderPositions = List.of(
new OrderPosition(new Product(&quot;Szynka&quot;, new BigDecimal(&quot;25&quot;),BigDecimal.ZERO, Category.A), 10),
new OrderPosition(new Product(&quot;Ser&quot;, new BigDecimal(&quot;25.5&quot;),BigDecimal.ZERO, Category.B), 20),
new OrderPosition(new Product(&quot;Chleb&quot;, new BigDecimal(&quot;15.5&quot;),BigDecimal.ZERO, Category.C), 10),
new OrderPosition(new Product(&quot;Nabial&quot;, new BigDecimal(&quot;8&quot;),BigDecimal.ZERO, Category.D), 2),
new OrderPosition(new Product(&quot;Kielbasa&quot;, new BigDecimal(&quot;15&quot;),BigDecimal.ZERO, Category.A), 10),
new OrderPosition(new Product(&quot;Jajko&quot;, new BigDecimal(&quot;5&quot;),BigDecimal.ZERO, Category.B), 5)
);
Optional&lt;BigDecimal&gt; op = orderPositions
.stream()
.filter(orderPosition -&gt; orderPosition.getProduct().getCategory().equals(Category.A))
.reduce((x,y) -&gt; x.add.y);

Thanks in advance!!

答案1

得分: 2

将映射到大十进制流,然后进行缩减操作:

BigDecimal op = orderPositions
                .stream()
                .filter(orderPosition -> Category.A.equals(orderPosition.getProduct().getCategory()))
                .map(orderPosition1 -> orderPosition1.getProduct().getPrice())
                .reduce(BigDecimal.ZERO, BigDecimal::add);

链接:
https://mkyong.com/java8/java-8-how-to-sum-bigdecimal-using-stream/
第一个值(BigDecimal.ZERO)是流为空时的默认值

请注意,我还更改了枚举的顺序,以避免空指针异常。

英文:

Map to big decimal stream then reduce

BigDecimal op = orderPositions
.stream()
.filter(orderPosition -&gt; Category.A.equals(orderPosition.getProduct().getCategory()))
.map(orderPosition1 -&gt; orderPosition1.getProduct().getPrice())
.reduce(BigDecimal.ZERO, BigDecimal::add);

Link:
https://mkyong.com/java8/java-8-how-to-sum-bigdecimal-using-stream/
The first value(BigDecimal.ZERO) is the default value in case stream is empty

Note that I also change to equal order of the Enum, to avoid nullPointerException

huangapple
  • 本文由 发表于 2020年9月21日 23:59:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63995914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定