使用 Stream 对 List 中的 Object 字段求和
Using Stream to sum Object fields in List
我正在尝试对列表的字段和 return 值求和。我想为此使用流,但我是流的新手,不确定流是否可以完成此操作。这是我试过的方法,但我认为语法不正确。
public double calculateCartTotal(ArrayList cartItems) {
this.totalPrice = cartItems.stream()
.map(item -> item.getTotalItemPrice())
.reduce(0, (a, b) -> a + b);
return totalPrice;
}
上下文的相关 class 结构。
public class Cart {
private double totalPrice;
private List<CartItem> cartItems;
public Cart() {
super();
this.totalPrice = 0;
this.cartItems = new ArrayList<CartItem>();
}
//other methods
}
public class CartItem {
private Product productName;
private int numberOfUnits;
private double totalItemPrice;
private double unitPrice;
public CartItem(Product productName, int numberOfUnits) {
super();
this.productName = productName;
this.numberOfUnits = numberOfUnits;
}
//other methods
}
获取总价和单价的方法
public double getTotalItemPrice() {
return this.getUnitPrice() * numberOfUnits;
}
public double getUnitPrice() {
return Double.parseDouble(productName.getCurrentPrice());
}
您需要将 cartItems
参数声明为 List<CartItem>
:
public double calculateCartTotal(List<CartItem> cartItems) {
this.totalPrice = cartItems.stream()
.mapToDouble(CartItem::getTotalItemPrice)
.sum();
return totalPrice;
}
您的代码有 2 个问题。
ArrayList
缺少类型参数。这是有问题的,因为现在我们不知道列表是否真的包含 CartItem
s。此外,您通常希望避免在声明中使用集合的实现,例如List<CartItem> items = new ArrayList<>();
好多了。
未将流转换为 DoubleStrem
。使用 DoubleStream
的优点是它不会将原始双精度转换为 Double
对象。此外,与普通 Stream
不同,它可以处理数字,因此它带有有用的方法,例如 sum
,我们不必使用 reduce
.
示例代码
public double calculateCartTotal(List<CartItem> cartItems) {
this.totalPrice = cartItems.stream()
.mapToDouble(i -> i.getTotalItemPrice())
.sum();
return totalPrice;
}
我正在尝试对列表的字段和 return 值求和。我想为此使用流,但我是流的新手,不确定流是否可以完成此操作。这是我试过的方法,但我认为语法不正确。
public double calculateCartTotal(ArrayList cartItems) {
this.totalPrice = cartItems.stream()
.map(item -> item.getTotalItemPrice())
.reduce(0, (a, b) -> a + b);
return totalPrice;
}
上下文的相关 class 结构。
public class Cart {
private double totalPrice;
private List<CartItem> cartItems;
public Cart() {
super();
this.totalPrice = 0;
this.cartItems = new ArrayList<CartItem>();
}
//other methods
}
public class CartItem {
private Product productName;
private int numberOfUnits;
private double totalItemPrice;
private double unitPrice;
public CartItem(Product productName, int numberOfUnits) {
super();
this.productName = productName;
this.numberOfUnits = numberOfUnits;
}
//other methods
}
获取总价和单价的方法
public double getTotalItemPrice() {
return this.getUnitPrice() * numberOfUnits;
}
public double getUnitPrice() {
return Double.parseDouble(productName.getCurrentPrice());
}
您需要将 cartItems
参数声明为 List<CartItem>
:
public double calculateCartTotal(List<CartItem> cartItems) {
this.totalPrice = cartItems.stream()
.mapToDouble(CartItem::getTotalItemPrice)
.sum();
return totalPrice;
}
您的代码有 2 个问题。
ArrayList
缺少类型参数。这是有问题的,因为现在我们不知道列表是否真的包含CartItem
s。此外,您通常希望避免在声明中使用集合的实现,例如List<CartItem> items = new ArrayList<>();
好多了。未将流转换为
DoubleStrem
。使用DoubleStream
的优点是它不会将原始双精度转换为Double
对象。此外,与普通Stream
不同,它可以处理数字,因此它带有有用的方法,例如sum
,我们不必使用reduce
.
示例代码
public double calculateCartTotal(List<CartItem> cartItems) {
this.totalPrice = cartItems.stream()
.mapToDouble(i -> i.getTotalItemPrice())
.sum();
return totalPrice;
}