Skip to content
Related Articles

Related Articles

Improve Article

Properties computeIfAbsent(Key, Function) method in Java with Examples

  • Last Updated : 23 May, 2019
Geek Week

The computeIfAbsent(Key, Function) method of Properties class which allows you to compute value of a mapping for specified key if key is not already associated with a value (or is mapped to null).

  • If mapping function of this method returns null, then no mapping is recorded.
  • If the 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 Object computeIfAbsent?(Object key,
                              Function mappingFunction)

Parameters: This method accepts two parameters:

  • key: key with which the value is to be associated.
  • 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.

Exception: This method throws ConcurrentModificationException if it is detected that the remapping function modified this map.



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 properties and add some values
        Properties properties = new Properties();
        properties.put("Pen", 10);
        properties.put("Book", 500);
        properties.put("Clothes", 400);
        properties.put("Mobile", 5000);
  
        // print Properties details
        System.out.println("Current Properties: "
                           + properties.toString());
  
        // provide value for new key which is absent
        // using computeIfAbsent method
        properties.computeIfAbsent("newPen", k -> 600);
        properties.computeIfAbsent("newBook", k -> 800);
  
        // print new mapping
        System.out.println("New Properties: "
                           + properties.toString());
    }
}
Output:
Current Properties: {Book=500, Mobile=5000, Pen=10, Clothes=400}
New Properties: {newPen=600, Book=500, newBook=800, Mobile=5000, Pen=10, Clothes=400}

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 properties and add some values
        Properties properties = new Properties();
  
        properties.put(1, "100RS");
        properties.put(2, "500RS");
        properties.put(3, "1000RS");
  
        // print Properties details
        System.out.println("Current Properties: "
                           + properties.toString());
  
        // provide value for new key which is absent
        // using computeIfAbsent method
        properties.computeIfAbsent(4, k -> "600RS");
  
        // this will not effect anything
        // because key 1 is present
        properties.computeIfAbsent(1, k -> "800RS");
  
        // print new mapping
        System.out.println("New Properties: "
                           + properties.toString());
    }
}
Output:
Current Properties: {3=1000RS, 2=500RS, 1=100RS}
New Properties: {4=600RS, 3=1000RS, 2=500RS, 1=100RS}

References: https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html#computeIfAbsent-java.lang.Object-java.util.function.Function-

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :