Java Hashmap put() method

In this tutorial we will learn about Java HashMap Put method . We will see how to  hashmap.put() method add element in map , we will also see value associated with the previous key example . Let’s see java hashmap put examples one by one .

Java HashMap.put() Syntax

Java HashMap Put method used to insert key-value pair in map . It return the previous value associated with specific  key, or null if there was no mapping for key in HashMap.

Syntax :

hashmap.put(K key, V value)

Parameters:
key : The key with which the mapping has to be associated with in the HashMap.
value :  value for the specified key in HashMap.
Returns:
the previous value associated with specific key, or null if there was no mapping for key in HashMap.

Example 1 –  Java HashMap put() – Key Not Present

In this example , we will initialize HashMap with key-value pair . After  that we will add element to map java with key value pair put(5,”E”) . Because key 5 is not present , So new entry will  be added to HashMap .

import java.util.HashMap;

public class JavaHashmapPut {
	public static void main(String[] args) {
	
		HashMap<Integer, String> hashMap = new HashMap<>();
        //add element in HashMap put method 
		hashMap.put(1, "A");
        hashMap.put(2, "B");
        hashMap.put(3, "C");
        hashMap.put(4, "D");
        
        System.out.println("HashMap before put : " + hashMap);t
        //add element in hashmap with new key 5  
        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}

Example 2 –  Java HashMap put() – Key Present

In this example , we will initialize HashMap with key-value pair . After that we will add  element to hashmap java with duplicate key value pair put(2,”E”) . Because key 2 is present , So it will update value of key in map .

import java.util.HashMap;

public class JavaHashmapPut1 {
	
	public static void main(String[] args) {
	
		HashMap<Integer, String> hashMap = new HashMap<>();
        //add element in HashMap put method 
		hashMap.put(1, "A");
        hashMap.put(2, "B");
        hashMap.put(3, "C");
        hashMap.put(4, "D");
        
        System.out.println("HashMap before put : " + hashMap);
        //add element in hashmap with duplicate  key 2  
        hashMap.put(2, "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=E, 3=C, 4=D}

Example 3 –  Java HashMap put() – Retrun Value

In this example , we will initialize HashMap with key-value pair . After that we will add  element to hashmap java with duplicate key value pair put(2,”E”) . Because key 2 is present , So it will update value of key in map and return existing association for the key 2 with value B. Key 5 is not present in HashMap it will add new entry in HashMap and return null.

import java.util.HashMap;

public class JavaHashmapPut2 {
	
	public static void main(String[] args) {
			
		HashMap<Integer, String> hashMap = new HashMap<>();
        //add element in HashMap put method 
		hashMap.put(1, "A");
        hashMap.put(2, "B");
        hashMap.put(3, "C");
        hashMap.put(4, "D");

        //add element in hashmap with duplicate  key 5
        String returnValue = hashMap.put(2, "E");
        
        System.out.println("HashMap  put method retrun value for key 2 : " + returnValue);
       
        //add element in hashmap with new  key 5  
        returnValue =  hashMap.put(5, "E");
        
        System.out.println("HashMap  put method retrun value for key 5 : " + returnValue);
        
		
	}
}

Output:

HashMap  put method retrun value for key 2 : B
HashMap  put method retrun value for key 5 : null

In this tutorial we learned different example of java hashmap put() method .  We have seen java hashmap add example when key is present or key is not present . We also learned return existing association for the key of HashMap in Java . You can learn more java HashMap examples for practice .

Leave a Reply

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

81 − 80 =