Now there is a shorter way with Java 8 using Map::merge
.
myMap.merge(key, 1, Integer::sum)
What it does:
- if key do not exists, put 1 as value
- otherwise sum 1 to the value linked to key
More information here.
More Related Contents:
- What is the difference between "collection" ,"Collection" and "Collections"? [duplicate]
- Difference between
- Properly removing an Integer from a List
- In ArrayBlockingQueue, why copy final member field into local final variable?
- Fastest way to determine if an integer’s square root is an integer
- Where do I find a standard Trie based map implementation in Java? [closed]
- What is the best way to combine two lists into a map (Java)?
- Why to use StringBuffer in Java instead of the string concatenation operator
- Hibernate Validation of Collections of Primitives
- What is the best way get the symmetric difference between two sets in java?
- Performance ConcurrentHashmap vs HashMap
- What are Reified Generics? How do they solve Type Erasure problems and why can’t they be added without major changes?
- What is the difference between an ordered and a sorted collection?
- Java – How to create new Entry (key, value)
- How to modify a Collection while iterating using for-each loop without ConcurrentModificationException? [duplicate]
- Sort a List of objects by multiple fields [duplicate]
- Empty an ArrayList or just create a new one and let the old one be garbage collected? [duplicate]
- Removing items from a collection in java while iterating over it
- Most efficient way of converting String to Integer in java
- what’s a good persistent collections framework for use in java?
- Is there a way to get the value of a HashMap randomly in Java?
- Difference between a deprecated and a legacy API?
- Any implementation of Ordered Set in Java?
- JDK9 randomization on immutable sets and maps
- What Java 8 Stream.collect equivalents are available in the standard Kotlin library?
- Create a List of primitive int?
- Java collections covariance problem
- ‘MergeSort Algorithm’ – What’s the better implementation in JAVA? [closed]
- Java for-loop optimization
- How to do map inversion with Guava with non-unique values?