The TreeMap.computeIfPresent()
method in Java is used to compute a new mapping for a specified key if it is already associated with a value. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. We will also cover a real-world use case to show how TreeMap.computeIfPresent()
can be used effectively.
Table of Contents
- Introduction
computeIfPresent
Method Syntax- Examples
- Computing a New Mapping for an Existing Key
- Handling Non-Existent Keys
- Real-World Use Case
- Example: Updating Student Scores
- Conclusion
Introduction
The TreeMap.computeIfPresent()
method is a member of the TreeMap
class in Java. It allows you to compute a new mapping for a specified key if it is already associated with a value. If the key is not present, the method does nothing.
computeIfPresent() Method Syntax
The syntax for the computeIfPresent
method is as follows:
public V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
- Parameters:
key
: The key with which the specified value is to be associated.remappingFunction
: The function to compute a new value.
- Returns: The new value associated with the specified key, or
null
if the key is not present or the computed value isnull
.
Examples
Computing a New Mapping for an Existing Key
The computeIfPresent
method can be used to compute a new mapping for an existing key in the TreeMap
.
Example
import java.util.TreeMap;
import java.util.function.BiFunction;
public class ComputeIfPresentExample {
public static void main(String[] args) {
// Creating a TreeMap with String keys and Integer values
TreeMap<String, Integer> treeMap = new TreeMap<>();
// Adding entries to the TreeMap
treeMap.put("Ravi", 25);
treeMap.put("Priya", 30);
treeMap.put("Vijay", 35);
treeMap.put("Anita", 28);
treeMap.put("Suresh", 40);
// Computing a new mapping for the key "Priya" if it is present
treeMap.computeIfPresent("Priya", (key, value) -> value + 10);
// Printing the TreeMap after computing a new mapping
System.out.println("TreeMap after computing new mapping for 'Priya': " + treeMap);
}
}
Output:
TreeMap after computing new mapping for 'Priya': {Anita=28, Priya=40, Ravi=25, Suresh=40, Vijay=35}
Handling Non-Existent Keys
If the key is not present in the TreeMap
, the computeIfPresent
method does nothing.
Example
import java.util.TreeMap;
import java.util.function.BiFunction;
public class NonExistentKeyExample {
public static void main(String[] args) {
// Creating a TreeMap with String keys and Integer values
TreeMap<String, Integer> treeMap = new TreeMap<>();
// Adding entries to the TreeMap
treeMap.put("Ravi", 25);
treeMap.put("Priya", 30);
treeMap.put("Vijay", 35);
treeMap.put("Anita", 28);
treeMap.put("Suresh", 40);
// Attempting to compute a new mapping for the key "Amit" which is not present
treeMap.computeIfPresent("Amit", (key, value) -> value + 10);
// Printing the TreeMap after attempting to compute a new mapping for a non-existent key
System.out.println("TreeMap after attempting to compute new mapping for 'Amit': " + treeMap);
}
}
Output:
TreeMap after attempting to compute new mapping for 'Amit': {Anita=28, Priya=30, Ravi=25, Suresh=40, Vijay=35}
Real-World Use Case
Example: Updating Student Scores
A common real-world use case for TreeMap.computeIfPresent()
is updating student scores in a map of student names and their scores.
Example
import java.util.TreeMap;
import java.util.function.BiFunction;
public class StudentScoresManager {
public static void main(String[] args) {
// Creating a TreeMap to manage student names and their scores
TreeMap<String, Integer> studentScores = new TreeMap<>();
// Adding student names and their scores to the TreeMap
studentScores.put("Ravi", 85);
studentScores.put("Priya", 90);
studentScores.put("Vijay", 75);
studentScores.put("Anita", 95);
studentScores.put("Suresh", 80);
// Updating the score of "Priya" if present
studentScores.computeIfPresent("Priya", (key, value) -> value + 5);
// Printing the updated student scores
System.out.println("Updated student scores: " + studentScores);
}
}
Output:
Updated student scores: {Anita=95, Priya=95, Ravi=85, Suresh=80, Vijay=75}
In this example, TreeMap.computeIfPresent()
is used to update the score of a student if their name is present in the map, making it easy to manage and update student scores.
Conclusion
The TreeMap.computeIfPresent()
method in Java provides a way to compute a new mapping for a specified key if it is already associated with a value. By understanding how to use this method, you can efficiently manage and navigate collections of key-value pairs in your Java applications. The method allows you to update values for existing keys based on custom logic, making it a versatile tool for data management in various scenarios.