nimo23
nimo23

Reputation: 5708

modify value of map with forEach

I have a HashMap and want to change the value (which is a string) by appending another string "hello".

HashMap<User, String> all = new HashMap<>();
mymap.forEach((k, v) -> v = v + " hello");

However, it does not work, "mymap" is unchanged. What is wrong?

Upvotes: 10

Views: 8550

Answers (1)

Misha
Misha

Reputation: 28183

This is the job for Map#replaceAll:

mymap.replaceAll((k, v) -> v + " hello");

Upvotes: 18

Related Questions