HashMap computeIfAbsent() method in Java with Examples
The computeIfAbsent(Key, Function) method of HashMap class is used to compute value for a given key using the given mapping function, if key is not already associated with a value (or is mapped to null) and enter that computed value in Hashmap else null.
- If mapping function of this method returns null, then no mapping is recorded for that key.
- At time of computation if remapping function throws an exception, the exception is rethrown, and the no mapping is recorded.
- During computation, modification this map using this method is not allowed.
- This method will throw a ConcurrentModificationException if the remapping function modified this map during computation.
Syntax:
public V computeIfAbsent(K key, Function<? super K, ? extends V> remappingFunction)
Parameters: This method accepts two parameters:
- key : key for which we want to compute value using mapping.
- remappingFunction : function to do the operation on value.
Returns: This method returns current (existing or computed) value associated with the specified key, or null if mapping returns null.
Below programs illustrate the computeIfAbsent(Key, Function) method:
Program 1:
// Java program to demonstrate // computeIfAbsent(Key, Function) method. import java.util.*; public class GFG { // Main method public static void main(String[] args) { // create a HashMap and add some values HashMap<String, Integer> map = new HashMap<>(); map.put( "key1" , 10000 ); map.put( "key2" , 55000 ); map.put( "key3" , 44300 ); map.put( "key4" , 53200 ); // print map details System.out.println( "HashMap:\n " + map.toString()); // provide value for new key which is absent // using computeIfAbsent method map.computeIfAbsent( "key5" , k -> 2000 + 33000 ); map.computeIfAbsent( "key6" , k -> 2000 * 34 ); // print new mapping System.out.println( "New HashMap:\n " + map); } } |
HashMap: {key1=10000, key2=55000, key3=44300, key4=53200} New HashMap: {key1=10000, key2=55000, key5=35000, key6=68000, key3=44300, key4=53200}
Program 2:
// Java program to demonstrate // computeIfAbsent(Key, Function) method. import java.util.*; public class GFG { // Main method public static void main(String[] args) { // create a HashMap and add some values HashMap<Integer, String> map = new HashMap<>(); map.put( 10 , "Aman" ); map.put( 20 , "Suraj" ); map.put( 30 , "Harsh" ); // print map details System.out.println( "HashMap:\n" + map.toString()); // provide value for new key which is absent // using computeIfAbsent method map.computeIfAbsent( 40 , k -> "Sanjeet" ); // this will not effect anything // because key 1 is present map.computeIfAbsent( 10 , k -> "Amarjit" ); // print new mapping System.out.println( "New HashMap:\n" + map); } } |
HashMap: {20=Suraj, 10=Aman, 30=Harsh} New HashMap: {20=Suraj, 40=Sanjeet, 10=Aman, 30=Harsh}
Recommended Posts:
- ConcurrentHashMap computeIfAbsent() method in Java with Examples
- Hashtable computeIfAbsent() method in Java with Examples
- Properties computeIfAbsent(Key, Function) method in Java with Examples
- HashMap compute() method in Java with Examples
- HashMap putIfAbsent(key, value) method in Java with Examples
- HashMap replace(key, value) method in Java with Examples
- HashMap replaceAll(BiFunction) method in Java with Examples
- HashMap computeIfPresent(key, BiFunction) method in Java with Examples
- HashMap getOrDefault(key, defaultValue) method in Java with Examples
- HashMap merge(key, value, BiFunction) method in Java with Examples
- HashMap forEach(BiConsumer) method in Java with Examples
- HashMap replace(key, oldValue, newValue) method in Java with Examples
- HashMap in Java with Examples
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet