Java HashMap computeifPresent()

Java ComputeIfpresent()

Java HashMap

  • Java HashMap computeIfPresent() is a method in the Java HashMap class that updates the value of a key in the HashMap if the key is present in the HashMap.

  • The method takes two parameters, a key and a BiFunction object that takes the key and the current value of the key as input and returns a new value for the key.

  • To understand the Java HashMap Compute() Method, Read the Complete Article.

The computeifPresent() method performs the following operations:

  • Here are the methods to perfrom following operations.
  • Checks if the specified key is present in the map. If the key is not present, the computeIfPresent() method does nothing and returns null.
  • If the key is present, the computeIfPresent() method retrieves the current value associated with the key.
  • The computeIfPresent() method applies the specified function (passed as the second argument) to the key and current value. The function must return the new value for the key.
  • The computeIfPresent() method then associates the new value with the key in the map and returns the new value.
  • If the function passed to computeIfPresent() returns null, the key is removed from the map.
public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
V computeIfPresent(K key, BiFunction remappingFunction)

where K is the type of the key, V is the type of the value, and remappingFunction is the function used to compute the new value.

Java Computeifpresent() Method

Let’s look at the Java HashMap computeifpresent() Method to perform certain operations.

Example 1: Java Program to run use computeifpresent Method.

Run
import java.util.HashMap;

public class Main {

    public static void main(String[] args) {
        // Create a HashMap with some initial mappings
        HashMap<String, Integer> map = new HashMap<>();
        map.put("ayush", 1);
        map.put("ashish", 2);

        // Insert a new value using the compute() method
        map.compute("anjali", (key, value) -> value == null ? 3 : value);

        // Print the updated HashMap
        System.out.println(map);
    }
}

Output

{banana=2, apple=2}

Example 2 :Java Program to use Computeifpresent() Method

Run
import java.util.HashMap;

import java.util.Map;

 
public class Main
{
  
public static void main (String[]args)
  {
    
      // Create a new HashMap
      Map < String, Integer > map = new HashMap <> ();
    
 
      // Add some key-value pairs
      map.put ("apple", 1);
    
map.put ("banana", 2);
    
map.put ("cherry", 3);
    
 
      // Compute a new value for an existing key
      map.computeIfPresent ("banana", (key, value)->value * 2);
    
 
      // Print the updated map
      System.out.println (map);

} 
} 

Output

{banana=4, apple=1, cherry=3}

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription