I have a Map that has all these values :
Map :
B Bonus
C Cash
D Draft
And I have a list of Lists : Account
:
Number, Type, Name
AccountsList
Account 0 : 123, B, Sam
Account 1 : 124, C, Harry
I have to update the values in AccountList based on Keyvalue pair in Map.
I have the below code:
for (Account acc: AccountList) {
acc.setAccountType(map.get(acc.getAccountType()));
}
How do I write the above code using Java 8 streams
您可以尝试使用forEach
:
AccountList.stream().forEach(acc -> acc.setAccountType(map.get(acc.getAccountType())))
you can do this by parallelStream:
AccountList =
AccountList.
parallelStream().
peek(
acc -> acc.setAccountType(
map.get(acc.getAccountType())
)
).collect(
Collectors.toList()
);
if you want to return a new List without modifying the existing one
List<AccountList> updatedAccountList = accountLists.stream().map(a -> {
AccountList newAccountList = new AccountList();
newAccountList.setType(map.get(a.getType()));
return newAccountList;
}).collect(Collectors.toList());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.