Java HashMap 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.
The syntax of the computeIfPresent() method is:
public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
compute() Parameters :
The computeIfPresent() method of the HashMap class in Java takes two parameters: a key and a BiFunction object.
Here's the signature of the computeIfPresent() method:
V computeIfPresent(K key, BiFunction super K, ? super V, ? extends V> 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.
compute() Return Value:
The computeIfPresent() method in Java's HashMap class returns the new value associated with the specified key after applying the specified function. If the key is not present in the map, the method returns null.
The return value of the computeIfPresent() method is determined by the function that is passed as the second argument to the method. The function takes two arguments, the key and the current value associated with the key, and returns the new value for the key. If the function returns null, the key is removed from the map.
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}
Explanation:
In this implementation, we create a HashMap object map with two key-value pairs.
We then call the computeIfPresent() method with the key "apple" and a lambda expression that increments the current value associated with the key by 1.
This updates the value of the "apple" key in the map to 2. Finally, we print the entire map using the toString method, which prints {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}
Explanation:
In this example, we create a new HashMap and add some key-value pairs to it.
We then call computeIfPresent() method on the map to compute a new value for the key "banana" using a lambda expression that multiplies the current value by 2. Finally, we print the updated map to see the changes made by the method.
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