Javatpoint Logo
Javatpoint Logo

Java ConcurrentHashMap replace() Method

The replace() method of ConcurrentHashMap class replaces the entry for a key only if it is currently mapped to some value.

Syntax

Parameter

key - key with which the specified value is associated

oldValue - value expected to be associated with the specified key

newValue - value to be associated with the specified key

Returns

true if the value was replaced

Throw

NullPointerException.

Example 1

Test it Now

Output:

HashMap values :
 {k1=100, k2=200, k3=300, k4=400}
New HashMap after remove :
 {k1=1000, k2=200, k3=300, k4=400}

Example 2

Test it Now

Output:

HashMap values :
 {k1=100, k2=200, k3=300, k4=400}
New HashMap after remove :
 {k1=100, k2=2000, k3=300, k4=400}





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA