Easy Tutorial
❮ Java Override Overload Data Search ❯

Java HashMap computeIfAbsent() Method

Java HashMap

The computeIfAbsent() method recalculates the value for a specified key in the hashMap if it is not already present, and adds it to the hashMap.

The syntax for the computeIfAbsent() method is:

hashmap.computeIfAbsent(K key, Function remappingFunction)

Note: hashmap is an object of the HashMap class.

Parameter Description:

Return Value

If the value for the key does not exist, it uses the remappingFunction to calculate the new value and stores it as the value for the key; otherwise, it returns the existing value.

Example

The following example demonstrates the use of the computeIfAbsent() method:

Example

import java.util.HashMap;

class Main {
    public static void main(String[] args) {
        // Create a HashMap
        HashMap&lt;String, Integer> prices = new HashMap<>();

        // Add mappings to the HashMap
        prices.put("Shoes", 200);
        prices.put("Bag", 300);
        prices.put("Pant", 150);
        System.out.println("HashMap: " + prices);

        // Calculate the price for Shirt
        int shirtPrice = prices.computeIfAbsent("Shirt", key -> 280);
        System.out.println("Price of Shirt: " + shirtPrice);

        // Print the updated HashMap
        System.out.println("Updated HashMap: " + prices);
    }
}

Executing the above program outputs:

HashMap: {Pant=150, Bag=300, Shoes=200}
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)

In the code, we used the lambda expression key -> 280 as the remapping function. The prices.computeIfAbsent() method associates the new value returned by the lambda expression with "Shirt".

Since "Shirt" does not exist in the HashMap, a new key/value pair is added.

For more information about lambda expressions, visit Java Lambda Expressions.

When the key already exists:

Example

import java.util.HashMap;

class Main {
    public static void main(String[] args) {
        // Create a HashMap
        HashMap&lt;String, Integer> prices = new HashMap<>();

        // Add mappings to the HashMap
        prices.put("Shoes", 180);
        prices.put("Bag", 300);
        prices.put("Pant", 150);
        System.out.println("HashMap: " + prices);

        // The mapping for Shoes already exists
        // No new value is calculated for Shoes
        int shoePrice = prices.computeIfAbsent("Shoes", (key) -> 280);
        System.out.println("Price of Shoes: " + shoePrice);

        // Print the updated HashMap
        System.out.println("Updated HashMap: " + prices);
    }
}

Executing the above program outputs:

HashMap: {Pant=150, Bag=300, Shoes=180}
Price of Shoes: 180
Updated HashMap: {Pant=150, Bag=300, Shoes=180}
// Output the updated HashMap
System.out.println("Updated HashMap: " + prices);
}
}

The output of the above program is:

HashMap: {Pant=150, Bag=300, Shoes=180}
Price of Shoes: 180
Updated HashMap: {Pant=150, Bag=300, Shoes=180}

In the above example, the mapping for Shoes already exists in the HashMap, so a new value is not calculated for Shoes.

Java HashMap ```

❮ Java Override Overload Data Search ❯