[英]Java nested lists in map returns an error when passing a list of lists to Map values
[英]Replace values in a List of Lists from a Map in Java 8
我有一個包含所有這些值的地圖:
地圖 :
B Bonus
C Cash
D Draft
我有一個列表列表: Account
:
Number, Type, Name
帳戶列表
Account 0 : 123, B, Sam
Account 1 : 124, C, Harry
我必須根據 Map 中的鍵值對更新 AccountList 中的值。
我有以下代碼:
for (Account acc: AccountList) {
acc.setAccountType(map.get(acc.getAccountType()));
}
如何使用 Java 8 流編寫上述代碼
您可以嘗試使用forEach
:
AccountList.stream().forEach(acc -> acc.setAccountType(map.get(acc.getAccountType())))
你可以通過parallelStream做到這一點:
AccountList =
AccountList.
parallelStream().
peek(
acc -> acc.setAccountType(
map.get(acc.getAccountType())
)
).collect(
Collectors.toList()
);
如果你想在不修改現有列表的情況下返回一個新列表
List<AccountList> updatedAccountList = accountLists.stream().map(a -> {
AccountList newAccountList = new AccountList();
newAccountList.setType(map.get(a.getType()));
return newAccountList;
}).collect(Collectors.toList());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.