英文:
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<OrderPosition> 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<BigDecimal> op = orderPositions
.stream()
.filter(orderPosition -> orderPosition.getProduct().getCategory().equals(Category.A))
.map(orderPosition -> 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<OrderPosition> 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<BigDecimal> op = orderPositions
.stream()
.filter(orderPosition -> orderPosition.getProduct().getCategory().equals(Category.A))
.reduce((x,y) -> 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 -> Category.A.equals(orderPosition.getProduct().getCategory()))
.map(orderPosition1 -> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论