Java HashMap Compute() Method
Java HashMap
- The compute() method in Java HashMap class is used to compute the value for a given key using the given remapping function.
- This method takes two parameters:
- key: This parameter is of the key type, which is used to find the value in the HashMap.
- remapping Function: This is a functional interface that is used to compute the new value for the key. It takes two parameters – key and value and returns the new value.
- To understand the Java HashMap Compute() Method, Read the Complete Article.
The compute() method performs the following operations:
- Here are the methods to perform following operations:
- It checks whether the key is present in the HashMap or not. If the key is not present, then it returns null.
- If the key is present, then it computes the new value using the given remapping function.
- If the new value is null, then it removes the mapping for the key from the HashMap.
- If the new value is not null, then it replaces the old value with the new value.
compute() Parameters :
The compute() method in Java's HashMap class takes two parameters:
key - This is the key whose value is to be computed.
remappingFunction - This is a functional interface that takes two parameters - the key and the current value associated with that key, and returns the new value to be associated with the key.
compute() Return Value:
The compute() method in Java's HashMap class returns the new value associated with the specified key, or null if there is no mapping for the key and the remapping function returns null.
If the remapping function returns a non-null value, the compute() method updates the mapping for the specified key with the new value returned by the function. If the remapping function returns null, the compute() method removes the mapping for the key from the HashMap.
Let’s look at the Java HashMap compute() Method to perform certain operations.
Example 1: Java Program to run use compute Method.
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
{ashish=2, anjali=3, ayush=1}
Explanation:
In this example, we create a HashMap with some initial mappings of "ayush" to 1 and "ashish" to 2.
We then use the compute() method to insert a new mapping of "anjali" to 3.
The remapping Function we pass to the compute() method simply checks if the current value is null, which it is because there is no mapping for "anjali", and returns 3.
The compute() method updates the HashMap with this new mapping.
Example 2 :Java Program to use Compute() Method
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 ("Cricket", 1); map.put ("Football", 2); // Insert a new value using the compute() method map.compute ("Basketball", (key, value)->value == null ? 3 : value); // Print the updated HashMap System.out.println (map); } }
Output
{Cricket=1, Basketball=3, Football=2}
Explanation:
In this example, we create a HashMap with some initial mappings of "cricket" to 1 and "football" to 2.
We then use the compute() method to insert a new mapping of "basketball" to 3.
The remappingFunction we pass to the compute() method simply checks if the current value is null, which it is because there is no mapping for "basketball", and returns 3.
The compute() method updates the HashMap with this new mapping.
As we can see, the HashMap now contains a mapping of "Basketball" to 3, which was inserted using the compute() 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