Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

putIfAbsent() – A Method Of Map Interface In Java

Posted on 03/21/2025 By admin

Previously we saw an important method of Map interface named getOrDefault(). In this post, we will see another important method named putIfAbsent().

Let’s start with an example:-

package Concepts; import java.util.HashMap;
import java.util.Map; public class PutIfAbsentMethodExample { public static void main(String[] args) { Map dataMap = new HashMap(); dataMap.put(101,"Amod"); dataMap.put(102,"Swati"); dataMap.put(103,"Aaditya"); dataMap.put(104,"Animesh"); // If we want to first check if a key is present and then add to map if not present if(!dataMap.containsKey(105)) dataMap.put(105, "Suresh"); // Or something as below if(dataMap.get(106) == null) dataMap.put(106, "Mahesh"); System.out.println(dataMap); } }

Output

{101=Amod, 102=Swati, 103=Aaditya, 104=Animesh, 105=Suresh, 106=Mahesh}

If I want to add a new key-value pair conditionally then we need to write some lines of code as above.

Now you have a direct method putIfAbsent() in Map interface that has similar implementation as above but now no need to write extra lines of code. It was added in 1.8.

If there is no value currently assosiated with given key then this method will add that key in Map with the given value and return null (You can see the implementation 742 Line no ). If the passed key has associated value then it will return that value.

package Concepts; import java.util.HashMap;
import java.util.Map; public class PutIfAbsentMethodExample { public static void main(String[] args) { Map dataMap = new HashMap(); dataMap.put(101,"Amod"); dataMap.put(102,"Swati"); dataMap.put(103,"Aaditya"); dataMap.put(104,"Animesh"); // If we want to first check if a key is present and then add to map if not present if(!dataMap.containsKey(105)) dataMap.put(105, "Suresh"); // Or something as below if(dataMap.get(106) == null) dataMap.put(106, "Mahesh"); System.out.println(dataMap); dataMap.putIfAbsent(107, "Sachin"); dataMap.putIfAbsent(108, "Virat"); System.out.println(dataMap); } } 
{101=Amod, 102=Swati, 103=Aaditya, 104=Animesh, 105=Suresh, 106=Mahesh}
{101=Amod, 102=Swati, 103=Aaditya, 104=Animesh, 105=Suresh, 106=Mahesh, 107=Sachin, 108=Virat}

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Tools To Find XPath In Chrome Browser
Next Post: How To Sort List In Ascending & Descending Order Using Java Stream API?

Related Posts

#1. OAuth 2.0 Flow – How Does It Work? Uncategorized
Why Should We Upcast Browser Driver Class Object To WebDriver? Uncategorized
REST Assured Tutorial 50 – How to set Content-Type for request in Rest Assured Uncategorized
loadurlOutput – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark