Map#putIfAbsent() in Java With Examples

In this tutorial you will see how to use putIfAbsent() method in Java HashMap. The putIfAbsent() method inserts the specified value if the passed key is not already present in the HashMap or the key is null. Using this method gives you an option to check if key is already present or not, before associating a value with the key. What this method does in a single statement can be explained as-

 V v = map.get(key);
 if (v == null)
     v = map.put(key, value);

 return v;

Syntax of putIfAbsent() method

V putIfAbsent(K key, V value)

Here the parameters are-

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

This method returns the previous value if key is already associated with a value or null if there was no existing mapping for the key. Method also returns null if key was previously inserted as null in the HashMap.

Note that the default implementation of the putIfAbsent() method in the Map interface makes no guarantees about synchronization or atomicity properties of this method. Implementation of putIfAbsent() in the ConcurrentHashMap guarantees atomicity.

putIfAbsent() Java examples

1. In the following example we’ll try to insert a new value with an existing key.

import java.util.HashMap;
import java.util.Map;

public class MapPutIfAbsent {

  public static void main(String[] args) {
      Map<String, String> carMap = new HashMap<String, String>();
      // Storing elements
      carMap.put("1", "Audi");
      carMap.put("2", "BMW");
      carMap.put("3", "Jaguar");
      String val = carMap.putIfAbsent("3", "Mini Cooper");
      System.out.println("Value is- " + val);
      System.out.println(carMap);
  }
}

Output

Value is- Jaguar
{1=Audi, 2=BMW, 3=Jaguar}

As you can see since the key is already present in the HashMap so the value is not rewritten. Also note that the putIfAbsent() is returning the already associated value.

2. In the following example we’ll try to insert a value with a key that doesn’t already exist.

public class MapPutIfAbsent {
  public static void main(String[] args) {
    Map<String, String> carMap = new HashMap<String, String>();
    // Storing elements
    carMap.put("1", "Audi");
    carMap.put("2", "BMW");
    carMap.put("3", "Jaguar");
    String val = carMap.putIfAbsent("4", "Mini Cooper");
    System.out.println("Value is- " + val);
    System.out.println(carMap);
  }
}

Output

Value is- null
{1=Audi, 2=BMW, 3=Jaguar, 4=Mini Cooper}

As you can see from the output value is added to the HashMap along with the new key. putIfAbsent() method returns null because there was no existing mapping for the key.

3. In the following example we’ll try to insert a value with a key as null when there is already a value with null key.

public class MapPutIfAbsent {

  public static void main(String[] args) {
    Map<String, String> carMap = new HashMap<String, String>();
      // Storing elements
      carMap.put("1", "Audi");
      carMap.put("2", "BMW");
      carMap.put("3", "Jaguar");
      carMap.put(null, "Volks Wagon");
      String val = carMap.putIfAbsent(null, "Mini Cooper");
      System.out.println("Value is- " + val);
      System.out.println(carMap);
  }
}

Output

Value is- Volks Wagon
{null=Volks Wagon, 1=Audi, 2=BMW, 3=Jaguar}

As you can see new value is inserted now since the previous value was mapped to null. Return value of the putIfAbsent() method is the previous value.

Related Posts

That’s all for the topic Map#putIfAbsent() in Java With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

Share this post:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.