从Java对象计数值?

从Java对象计数值?,第1张

从Java对象计数值

您可以利用Java 8 Stream API并实现如下所示的内容:

public static void main(String[] args) {    PaymentDetailsItem payment = new PaymentDetailsItem("test", "100.00", 10, "1");    PaymentDetailsItem payment2 = new PaymentDetailsItem("test number 2", "250.00", 10, "2");    List<PaymentDetailsItem> payments = new ArrayList<>();    payments.add(payment);    payments.add(payment2);    List<String> amounts = payments.stream().map(PaymentDetailsItem::getAmount).collect(Collectors.toList());    System.out.println("Here we have the extracted List of amounts: " + amounts);    String totalAmount = amounts.stream() .reduce((amount1, amount2) -> String.valueOf(Float.valueOf(amount1) + Float.valueOf(amount2))).get();    System.out.println("Total amount obtained by using .reduce() upon the List of amounts: " + totalAmount);    System.out.println("Or you can do everything at once: " + payments.stream().map(PaymentDetailsItem::getAmount) .reduce((amount1, amount2) -> String.valueOf(Float.valueOf(amount1) + Float.valueOf(amount2))).get());}

记住要为amount属性实现getter。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5013822.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存