Java HashMap.put() – Examples

In this tutorial, we will learn about the Java HashMap.put() function, and learn how to use this function to associate a value for the key in this HashMap, with the help of examples.

put()

HashMap.put() associates the specified value with the specified key in this map.

If the key is not already present, new entry is made. Else, existing value is updated.

The syntax of put() function is

put(K key, V value)

where

ParameterDescription
keyThe key with which the mapping has to be associated with, in the HashMap.
valueValue for the specified key.

Returns

The function returns previous associated value or null.

ADVERTISEMENT

Examples

1. put() – Key is not present in the HashMap

In this example, we will initialize a HashMap hashMap with some mappings in it. We will put (5, "E") key value pair into the hashMap. Since, the key 5 is not present in this HashMap, a new entry is made into the HashMap.

Java Program

import java.util.HashMap;

public class Example{ 
	public static void main(String[] args) {
		HashMap<Integer, String> hashMap = new HashMap<>();
		hashMap.put(1, "A");
		hashMap.put(2, "B");
		hashMap.put(3, "C");
		hashMap.put(4, "D");
		System.out.println("HashMap before put : " + hashMap);
		
		hashMap.put(5, "E");
		System.out.println("HashMap after  put : " + hashMap);
	}
}

Output

HashMap before put : {1=A, 2=B, 3=C, 4=D}
HashMap after  put : {1=A, 2=B, 3=C, 4=D, 5=E}

2. put() – Key is present in the HashMap

In this example, we will initialize a HashMap hashMap with some mappings in it. We will put (2, "b") key value pair into the hashMap. Since, the key 2 is present in this HashMap, the value is updated to "b".

Java Program

import java.util.HashMap;

public class Example{ 
	public static void main(String[] args) {
		HashMap<Integer, String> hashMap = new HashMap<>();
		hashMap.put(1, "A");
		hashMap.put(2, "B");
		hashMap.put(3, "C");
		hashMap.put(4, "D");
		System.out.println("HashMap before put : " + hashMap);
		
		hashMap.put(2, "b");
		System.out.println("HashMap after  put : " + hashMap);
	}
}

Output

HashMap before put : {1=A, 2=B, 3=C, 4=D}
HashMap after  put : {1=A, 2=b, 3=C, 4=D}

3. put() return value

In this example, we will print the returned value by put() method.

hashMap.put(2, "b") returns "B", since there is an existing association for the key 2 with value B.

hashMap.put(5, "E") returns null, since there is no existing association for the key 5.

Java Program

import java.util.HashMap;

public class Example{ 
	public static void main(String[] args) {
		HashMap<Integer, String> hashMap = new HashMap<>();
		hashMap.put(1, "A");
		hashMap.put(2, "B");
		hashMap.put(3, "C");
		hashMap.put(4, "D");
		
		String value = hashMap.put(2, "b");
		System.out.println("put() return value for key 2 : " + value);
		
		value = hashMap.put(5, "E");
		System.out.println("put() return value for key 5 : " + value);
	}
}

Output

put() return value for key 2 : B
put() return value for key 5 : null

4. put() – When HashMap is null

In this example, we will initialize a HashMap hashMap with null value and call put() method on this null object. put() method should throw java.lang.NullPointerException.

Java Program

import java.util.HashMap;

public class Example{ 
	public static void main(String[] args) {
		HashMap<Integer, String> hashMap = null;
		hashMap.put(2, "b");
	}
}

Output

Exception in thread "main" java.lang.NullPointerException
	at Example.main(Example.java:6)

Conclusion

In this Java Tutorial, we have learnt the syntax of Java HashMap.put() function, and also learnt how to use this function with the help of examples.