Home » Java ConcurrentHashMap compute() Method

Java ConcurrentHashMap compute() Method

by Online Tutorials Library

Java ConcurrentHashMap compute() Method

The compute() method of ConcurrentHashMap class is used to compute the mapping for the specified key and its current value.

Syntax

Parameter

key – key with which the specified value is to be associated.

remappingFunction – the function to compute a value.

Returns

the new value associated with the specified key, or null if none

Throws

NullPointerException

IllegalStateException

RuntimeException

Example 1

Test it Now

Output:

Values in map with detail:   {1=1, 2=10, 3=100, 4=2, 5=20, 6=200}  Map after using  compute(): {1=1, 2=10, 3=100, 4=2, 5=20, 6=200100}  

Example 2

Test it Now

Output:

ConcurrentHashMap value detail: {1=Java, 2=php, 3=.net}  New ConcurrentHashMap after compute: {1=Java, 2=php (Angular), 3=.net (java Script)}  

You may also like