Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java HashMap merge() Method

Java HashMap merge() Method

Java HashMap Java HashMap

The merge() method first checks whether the specified key exists; if not, it adds the key/value pair to the HashMap.

The syntax of the merge() method is:


hashmap.merge(key, value, remappingFunction)

**Note:** hashmap is an object of the HashMap class.

Parameter Description:

Return Value

If the value corresponding to the key does not exist, it returns that value; if it exists, it returns the value recalculated by the remappingFunction.

Example

The following example demonstrates the use of the merge() method:

Example

import

java.util.HashMap

;

class

Main

{

   

public

static

void

main

(

String

[

]

args

)

{

   

// Create a HashMap

    HashMap

<

String

, Integer

prices

=

new

HashMap

<>

(

)

;

   

// Insert a mapping into the HashMap

    prices.

put

(

"Shoes"

,

200

)

;

    prices.

put

(

"Bag"

,

300

)

;

    prices.

put

(

"Pant"

,

150

)

;

   

System

.

out

.

println

(

"HashMap: "

prices

)

;

   

int

returnedValue

=

prices.

merge

(

"Shirt"

,

100

,

(

oldValue, newValue

)

->

oldValue

newValue

)

;

   

System

.

out

.

println

(

"Price of Shirt: "

returnedValue

)

;

   

// Output the updated HashMap

   

System

.

out

.

println

(

"Updated HashMap: "

prices

)

;

   

}

}

The output of the above program is:


HashMap: {Pant=150, Bag=300, Shoes=200}
Price of Shirt: 100
Updated HashMap: {Pant=150, Shirt=100, Bag=300, Shoes=200}

In the above example, we created a HashMap named prices.

In the example above, we created a dynamic array named numbers.


prices.merge("Shirt", 100, (oldValue, newValue) -> oldValue + newValue)

In the code, we used the anonymous lambda expression (oldValue, newValue) -> oldValue + newValue as the remapping function.

Because the key Shirt is not in prices, the merge() method inserts the mapping Shirt=100 into prices, and the result of the remapping function is ignored.

To learn more about lambda expressions, please visit To learn more about anonymous functions, please visit.

The HashMap merge() method inserting a mapping with a duplicate key:

Example

import

java.util.HashMap

;

class

Main

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a HashMap

        HashMap

<

String

, String

countries

=

new

HashMap

<>

(

)

;

       

// Insert a mapping into the HashMap

        countries.

put

(

"Washington"

,

"America"

)

;

        countries.

put

(

"Canberra"

,

"Australia"

)

;

        countries.

put

(

"Madrid"

,

"Spain"

)

;

       

System

.

out

.

println

(

"HashMap: "

countries

)

;

       

// Merge the mapping for key Washington

       

String

returnedValue

=

countries.

merge

(

"Washington"

,

"USA"

,

(

oldValue, newValue

)

->

oldValue

"/"

newValue

)

;

       

System

.

out

.

println

(

"Washington: "

returnedValue

)

;

       

// Output the updated HashMap

       

System

.

out

.

println

(

"Updated HashMap: "

countries

)

;

   

}

}

The output of the above program is:

HashMap: {Madrid=Spain, Canberra=Australia, Washington=America}
Washington: America/USA
Updated HashMap: {Madrid=Spain, Canberra=Australia, Washington=America/USA},  

In the above example, we created a HashMap named countries.

In the example above, we created a dynamic array named numbers.

countries.merge("Washington", "USA", (oldValue, newValue) -> oldValue + "/" + newValue)

Here, we used the lambda expression (oldValue, newValue) -> oldValue + "/" + newValue as the remapping function.

Because the key washington already exists in countries, the old value is replaced by the value returned by the remapping function. Therefore, the mapping for Washington contains America/USA.

Java HashMap Java HashMap