我想遍历帐户列表,并使用Java 8 lambda功能总结Double信用额度.有任何想法吗?
import lombok.*;
import java.util.List;
@Data
public class Account {
private Double amount;
@Getter(AccessLevel.NONE)
private Boolean active;
private String companyCode;
private Long accountNumber;
public Boolean isActiveAccount() {
return active;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个帐户列表,我想使用Lambda Java 1.8功能总结信用额度,我该怎么做,而不是我在下面显示的传统方法?
//Lets pretend that the list has 3000 account-elements.
List<Account> accountsList = new ArrayList<>();
Double totalAmount = 0.0;
for (Account account : accountsList) {
Double accAmountList = account.getAmount();
if(null != accAmountList) totalAmount += accAmountList;
}
Run Code Online (Sandbox Code Playgroud)
从列表中创建一个流,映射到double值列表
total=accountList.stream()
.filter(account ->account!=null&&
account.getAmount()!=null).mapToDouble(Account::getAmount)
.sum()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3587 次 |
| 最近记录: |