How to increment the value of Map in one line in Java

How to write (Java 8 or later)

How to write

map.merge(key, 1, Integer::sum);

Try to move


jshell> Map<String, Integer> map = new HashMap<>();
map ==> {}

jshell> map.put("apple", 0);
$2 ==> null

jshell> map.get("apple");
$3 ==> 0

jshell> map.merge("apple", 1, Integer::sum);
$4 ==> 1

jshell> map.get("apple");
$5 ==> 1

jshell> map.merge("apple", 1, Integer::sum);
$6 ==> 2

jshell> map.get("apple");
$7 ==> 2


Let's examine the sum method of Integer and the merge method of Map


It's just adding two arguments.

sum public static int sum(int a, int b) Adds two integers, like the + operator. Parameters: a --First operand b --Second operand Return value: sum of a and b Introduced version: 1.8

Oracle documentation


default V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) If the specified key is not yet associated with a value or is associated with null, it will be associated with the specified non-null value. Otherwise, it replaces the relevant value with the result of the specified remapping function and removes it if the result is null.

Oracle documentation

In other words, the second argument value is the default value if it is null, otherwise the result of the function of the third argument is set.

The function with the third argument also takes two arguments. The first argument of the function of the third argument is the value for the key. The second argument of the function of the third argument is the value when the same value as the second argument is null.

[Java] Map # merge is hard to understand.

Is it like this when you give a name to a variable?

map.merge(key, defaultValue, (value, defaultValue) -> function());

In other words?

map.merge (key, 1, Integer :: sum) treats the default value like a step (the number added in one process), and sums the value and the default value with the sum of Integer by the method reference. I am.


Also, when I read "Effective Java 3rd Edition", this writing method was also mentioned in Item 43 Choosing a method reference over Lambda (P199).

Recommended Posts

How to increment the value of Map in one line in Java
[Java] How to get the maximum value of HashMap
How to derive the last day of the month in Java
[Java] How to get the key and value stored in Map by iterative processing
How to get the date in java
How to get the length of an audio file in java
[Java] How to get the authority of the folder
How to find the total number of pages when paging in Java
How to get the value after "_" in Windows batch like Java -version
How to change the value of a variable at a breakpoint in intelliJ
How to get the absolute path of a directory running in Java
Android development, how to check null in the value of JSON object
How to create your own annotation in Java and get the value
[Java] How to get the URL of the transition source
[Java] How to use Map
[Java] How to use Map
How to use Java Map
Summary of how to implement default arguments in Java
Conditional branching of the result of SQL statement to search only one in Java
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Summary of how to use the proxy set in IE when connecting with Java
How to get the class name / method name running in Java
How to retrieve the hash value in an array in Ruby
How to change the setting value of Springboot Hikari CP
How to switch Java in the OpenJDK era on Mac
How to learn JAVA in 7 days
How to use classes in Java?
How to name variables in Java
How to concatenate strings in java
How to specify an array in the return value / argument of the method in the CORBA IDL file
Considering the adoption of Java language in the Reiwa era ~ How to choose a safe SDK
Understand how functional programming was introduced to Java in one shot
How to output the value when there is an array in the array
How to write ruby if in one line Summary by beginner
Fix the file name of war to the one set in Maven
The milliseconds to set in /lib/ of Java jre is UTC
How to solve the unknown error when using slf4j in Java
How to check for the contents of a java fixed-length string
I want to change the value of Attribute in Selenium of Ruby
Get the result of POST in Java
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
Java reference to understand in the figure
[Java] How to use the hasNext function
[java] Summary of how to handle char
[Java] How to use the HashMap class
[Rails] How to use the map method
Reverse Key from Value in Java Map
How to do base conversion in Java
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to determine the number of parallels
[Java] How to set the Date time to 00:00:00
[Java] How to search for a value in an array (or list) with the contains method
[Java] How to get the current directory
How to implement coding conventions in Java
How to embed Janus Graph in Java
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class