Search code examples
javalistjava-8hashmapjava-stream

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 :

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


Solution

  • You can try with forEach:

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