Skip to main content
added 143 characters in body
Source Link
Roland Illig
  • 21.4k
  • 2
  • 34
  • 83

You must not modify the keys of a HashMap while iterating over it. You are just lucky it worked.

Instead of the put(...), write e.setValue(e.getValue() + 1).

If you have some kind of Multiset available (e.g. when you are using Guava), you could replace your HashMap with a Multiset<String>, which will make your intention clearer.

Using lambdas, your code would look like:

map.replaceAll((k, v) -> v + 1);

This looks very nice to me. It cannot get any shorter.

You must not modify the keys of a HashMap while iterating over it. You are just lucky it worked.

Instead of the put(...), write e.setValue(e.getValue() + 1).

If you have some kind of Multiset available (e.g. when you are using Guava), you could replace your HashMap with a Multiset<String>, which will make your intention clearer.

You must not modify the keys of a HashMap while iterating over it. You are just lucky it worked.

Instead of the put(...), write e.setValue(e.getValue() + 1).

If you have some kind of Multiset available (e.g. when you are using Guava), you could replace your HashMap with a Multiset<String>, which will make your intention clearer.

Using lambdas, your code would look like:

map.replaceAll((k, v) -> v + 1);

This looks very nice to me. It cannot get any shorter.

Source Link
Roland Illig
  • 21.4k
  • 2
  • 34
  • 83

You must not modify the keys of a HashMap while iterating over it. You are just lucky it worked.

Instead of the put(...), write e.setValue(e.getValue() + 1).

If you have some kind of Multiset available (e.g. when you are using Guava), you could replace your HashMap with a Multiset<String>, which will make your intention clearer.