English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The computeIfAbsent() method of Java HashMap calculates a new value and associates it with the specified key if the key is not associated with any value in the hash map.
The syntax of the computeIfAbsent() method is:
hashmap.computeIfAbsent(K key, Function remappingFunction)
The computeIfAbsent() method has two parameters:
key - Associated with the calculated value and the key
remappingFunction - For the specifiedKeyFunction to calculate a new value
Note:The remapping function cannot accept two parameters.
Returns the new value or old value associated with the specified key
Function to calculate a new value for the specified key
Note:If the remappingFunction result is null, the specifiedKeymapping.
import java.util.HashMap; class Main { public static void main(String[] args) { // Creating HashMap HashMap<String, Integer> prices = new HashMap<>(); // Inserting an entry into the HashMap prices.put("Shoes", 200); prices.put("Bag", 300); prices.put("Pant", 150); System.out.println("HashMap: ") + prices); // Calculate the value of shirt int shirtPrice = prices.computeIfAbsent("Shirt", (key -> 280); System.out.println("The price of shirt: ") + shirtPrice); // Print the updated HashMap System.out.println("Updated HashMap:") + prices); } }
Output Result
HashMap: {Pant=150, Bag=300, Shoes=200} The price of shirt: 280 Updated HashMap: {Pant=150, Shirt=280, Bag=300, Shoes=200}
In the above example, we created a hashmap named prices. Note the expression
prices.computeIfAbsent("Shirt", (key -> 280)
Here,
key -> 280 - It is a lambda expression. It returns the value28For more information about lambda expressions, please visitJava Lambda Expressions.
prices.computeIfAbsent() - Associate the new value returned by the lambda expression with the mapping of Shirt. This is possible because Shirt has not been mapped to any value in the hashmap yet.
import java.util.HashMap; class Main { public static void main(String[] args) { // Creating HashMap HashMap<String, Integer> prices = new HashMap<>(); // Inserting an entry into the HashMap prices.put("Shoes", 180); prices.put("Bag", 300); prices.put("Pant", 150); System.out.println("HashMap: ") + prices); //The mapping of Shoes already exists //Not to calculate the new value of Shoes int shoePrice = prices.computeIfAbsent("Shoes", (key) -> 280); System.out.println("Shoe price: ") + shoePrice); //Print the updated HashMap System.out.println("Updated HashMap:") + prices); } }
Output Result
HashMap: {Pant=150, Bag=300, Shoes=180} Shoes Price: 180 Updated HashMap: {Pant=150, Bag=300, Shoes=180}
In the above example, the mapping of Shoes already exists in the hash map. Therefore, the computeIfAbsent() method will not compute a new value for Shoes.
Recommended Reading
HashMap compute() - Compute the value of the specified key
HashMap computeIfPresent() - If the specified key is already mapped to a value, compute that value
Java HashMap merge() - Performs the same task as compute()