-1

How to loop a map elements and multiply value by lets say number 20 in java8 and get result?

I don't want to loop in usual for loop i want to make it efficient.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
nirmesh
  • 121
  • 6
  • 4
    why do you think a classic loop isn't efficient? – jhamon Jul 10 '18 at 13:29
  • 1
    Possible duplicate of [How to efficiently iterate over each entry in a 'Map'?](https://stackoverflow.com/questions/46898/how-to-efficiently-iterate-over-each-entry-in-a-map) – Bennett Dams Jul 10 '18 at 13:32

1 Answers1

3

You can use replaceAll if you want to mutate the map itself:

myMap.replaceAll((k, v) -> v * 20);

or collect to a new map:

myMap.entrySet()
     .stream()
     .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() * 20));
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126