Java Hashmap putIfAbsent() Method
Java Hashmap
Hashmap class is a part of java.util package. In hashmap we can store key and value pairs , only with unique keys.If we try to insert a duplicate key it will replace the old value with new value. Many built-in methods are there in hashmap class which makes it easy to perform operations on them, like insertion , deletion and many more. One of those methods is Java Hashmap putIfAbsent() Method.
To know more about Hashmap putIfAbsent() Method read the complete article.
Java Hashmap putIfAbsent() Method
The hashmap putIfAbsent() method is used add a key/value pair if the provided key is not present in the hashmap. This method returns two values. Below in this page you can find it’s syntax, return values, parameters with detailed examples.
Syntax :
new_hashmap.putIfAbsent(K key , V value);
Return values :
Null : If the provided key is not mapped in the hashmap.
Value : Value mapped to the key provided if key is already present in the hashmap.
Hashmap putIfAbsent() Method Examples
Example :
import java.util.*; public class Main { public static void main (String[]args) { // Creating an empty HashMap HashMap < String, Integer > hash_map_1 = new HashMap < String, Integer > (); // Mapping string values to int keys in a empty hashmap hash_map_1.put ("Maths", 80); hash_map_1.put ("English", 75); hash_map_1.put ("Hindi", 85); hash_map_1.put ("Science", 90); System.out.println ("Original hashmap : " + hash_map_1); //adding key/value mapping using putIfAbsent method System.out.println ("Present Value"+hash_map_1.putIfAbsent ("Computer", 80)); } }
Output :
Original hashmap : {Maths=80, English=75, Science=90, Hindi=85} null
Now putIfAbsent method is used to add new key/value pair if the provided key is absent and it returns null beacause key is absent in the hashmap.
Example :
import java.util.*; public class Main { public static void main (String[]args) { // Creating an empty HashMap HashMap < String, Integer > hash_map_1 = new HashMap < String, Integer > (); // Mapping string values to int keys in a empty hashmap hash_map_1.put ("Maths", 80); hash_map_1.put ("English", 75); hash_map_1.put ("Hindi", 85); hash_map_1.put ("Science", 90); System.out.println ("Original hashmap : " + hash_map_1); //adding key/value mapping using putIfAbsent method System.out.println ("Present value : " + hash_map_1.putIfAbsent ("Hindi", 80)); } }
Output :
Original hashmap : {Maths=80, English=75, Science=90, Hindi=85} Present value : 85
Now putIfAbsent method is used to add new key/value pair if the provided key is absent and it does not add the key/value pair and returns 85 beacause key is present in the hashmap.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment