Java Map put() putAll() putIfAbsent() Methods Tutorial

  • Post author:
  • Post category:Java Map

In this section we will learn what the Map put(), putAll(), and putIfAbsent() methods are and how to use them in Java.

What is Java Map put() Method?

The Java Map put() method is used to add a new entry (key and value) to a Map object.

Basically, this method takes two arguments (the key and value) and put them as a new entry in the target map object.

Java put() Method Syntax:

V put(K key, V value)

put() Method Parameters:

The method takes two arguments:

  • The first argument is the key that we want to add to the target Map object.
  • The second argument is the value we want to set for this specified key.

Note: if the key already is in the target Map object, then the first argument (key) will be ignored and only the value (the second argument) replaces the current value in the map object.

put() Method Return Value:

The return value of this method is the second argument of the method.

put() Method Exceptions:

ClassCastException: We get this exception if the datatype of either key or value is of incompatible type compared to the entries (keys or values) in the target Map object.

NullPointerException: We get this exception if we put a null argument for the key or value of the method but the target Map object doesn’t permit a null value.

Example: using Map put() method

import java.util.Map; 
import java.util.HashMap;
class Main{
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("Omid","Dehghan");
        map.put("Jack","Bauer");
        map.put("Barack","Obama");
        map.put("Elon","Musk");
        map.put("Lex","Fridman");

        map.forEach((k,v)->{
            System.out.println(k+" "+v);
        });
    }
}

Output:

Elon Musk

Barack Obama

Omid Dehghan

Jack Bauer

Lex Fridman

What is Java Map putAll() Method?

The Java Map putAll() method is used to copy the entire elements of a map object and put it in the object that this method is called with.

Java putAll() Method Syntax:

void putAll(Map<? extends K,? extends V> m)

putAll() Method Parameters:

The method takes one argument and that is a reference to a Map object we want to copy its elements and bring them into the current map object that this method is invoked with.

Note: if there was a key in the reference map object that this map also had it, only its value will be replaced with the new value from the reference object.

putAll() Method Return Value:

The return value of the method is void.

putAll() Method Exceptions:

UnsupportedOperationException: We get this exception if the method is not supported by the target Map object.

ClassCastException: We get this exception if the data type of either key or value is of incompatible type compared to the entries (key or value) in the target Map object.

NullPointerException: We get this exception if we put a null argument for the key or value or the method, but the target Map object doesn’t permit a null value.

IllegalArgumentException: We get this exception if one of the arguments has a property that doesn’t allow it to be an element in a Map object.

Example: using Map putAll() method

import java.util.Map; 
import java.util.HashMap;
class Main{
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("Omid","Dehghan");
        map.put("Jack","Bauer");
        map.put("Barack","Obama");
        map.put("Elon","Musk");
        map.put("Lex","Fridman");

        Map<String, String> map2 = new HashMap<>();
        map2.put("James","May");
        map2.put("Richard","Hammond");

        map.putAll(map2);

        map.forEach((k,v)->{
            System.out.println(k+" "+v);
        });
    }
}

Output:

James May

Elon Musk

Richard Hammond

Barack Obama

Omid Dehghan

Jack Bauer

Lex Fridman

What is Java Map putIfAbsent() Method?

The Java Map putIfAbsent() method is used to set a value for a key only if that key does not exist in the target Map object or is associated with a null value.

Otherwise, if the key is already in the target map and has a non-null value associates with it, then nothing will happen.

Java putIfAbsent() Method Syntax:

default V putIfAbsent(K key, V value)

putIfAbsent() Method Parameters:

The method takes two arguments:

  • The first argument is the key we want to search for in the target Map and see if there’s such key already in the map.
  • The second argument is the value we want to set for that key.

Note: this second argument only will be used if the target key was not in the map or it was associated with a null value.

putIfAbsent() Method Return Value:

The return value of this method, depending on whether the key was in the target map, is either:

The current value associated with the key in the target map object (if the key had a non-null value associated with)

Or the second argument we’ve passed to the method if the key was not in the map object or had a null for its value.

offerLast() Method Exceptions:

UnsupportedOperationException: We get this exception if the method is not supported by the target Map object.

ClassCastException: We get this exception if the data type of either key or value is of incompatible type compared to the entries (key or value) in the target Map object.

NullPointerException: We get this exception if we put a null argument for the key or value or the method, but the target Map object doesn’t permit a null value.

IllegalArgumentException: We get this exception if one of the arguments has a property that doesn’t allow it to be an element in a Map object.

Example: using Map putIfAbsent() method

import java.util.Map; 
import java.util.HashMap;
class Main{
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("Omid","Dehghan");
        map.put("Jack","Bauer");
        map.put("Barack","Obama");
        map.put("Elon","Musk");
        map.put("Lex","Fridman");

        map.putIfAbsent("Elon","Doe");
        map.putIfAbsent("John","Doe");

        map.forEach((k,v)->{
            System.out.println(k+" "+v);
        });
    }
}

Output:

Elon Musk

John Doe

Barack Obama

Omid Dehghan

Jack Bauer

Lex Fridman

Leave a Reply