简体   繁体   English

替换 Java 8 中地图列表中的值

[英]Replace values in a List of Lists from a Map in Java 8

I have a Map that has all these values :我有一个包含所有这些值的地图:

Map :地图 :

B Bonus
C Cash
D Draft

And I have a list of Lists : Account :我有一个列表列表: 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.我必须根据 Map 中的键值对更新 AccountList 中的值。

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如何使用 Java 8 流编写上述代码

您可以尝试使用forEach

AccountList.stream().forEach(acc -> acc.setAccountType(map.get(acc.getAccountType())))

you can do this by parallelStream:你可以通过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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM