To update the value for a key in a map in Java, you can use the put method or the replace method. These methods allow you to modify the value associated with a specific key directly.
We can update the value for a key in a map in Java using the put method, which associates the specified value with the specified key in the map.
For example,
java.util.HashMap
package, which provides the HashMap class needed to create and manipulate the map.myMap
with some key-value pairs. In this example, the map has integer keys and string values.put
method to update the value for a specific key in the map. The put
method takes the key and the new value to be assigned.System.out.println
.import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// Declare and initialize a map
Map<Integer, String> myMap = new HashMap<>();
myMap.put(1, "one");
myMap.put(2, "two");
myMap.put(3, "three");
// Update the value using put method
myMap.put(2, "TWO");
// Print the updated map
for (Map.Entry<Integer, String> entry : myMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
1: one 2: TWO 3: three
We can update the value for a key in a map in Java using the replace method, which replaces the value for a specified key only if it is already mapped.
For example,
java.util.HashMap
package, which provides the HashMap class needed to create and manipulate the map.myMap
with some key-value pairs. In this example, the map has integer keys and string values.replace
method to update the value for a specific key in the map. The replace
method takes the key and the new value to be assigned.System.out.println
.import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// Declare and initialize a map
Map<Integer, String> myMap = new HashMap<>();
myMap.put(1, "one");
myMap.put(2, "two");
myMap.put(3, "three");
// Update the value using replace method
myMap.replace(2, "TWO");
// Print the updated map
for (Map.Entry<Integer, String> entry : myMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
1: one 2: TWO 3: three
We can update the value for a key in a map in Java using the computeIfPresent method, which updates the value for a specified key only if it is already mapped.
For example,
java.util.HashMap
package, which provides the HashMap class needed to create and manipulate the map.myMap
with some key-value pairs. In this example, the map has integer keys and string values.computeIfPresent
method to update the value for a specific key in the map. The computeIfPresent
method takes the key and a function that computes the new value to be assigned.System.out.println
.import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// Declare and initialize a map
Map<Integer, String> myMap = new HashMap<>();
myMap.put(1, "one");
myMap.put(2, "two");
myMap.put(3, "three");
// Update the value conditionally using computeIfPresent method
myMap.computeIfPresent(2, (key, value) -> "TWO");
// Print the updated map
for (Map.Entry<Integer, String> entry : myMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
1: one 2: TWO 3: three
In this tutorial, we learned How to Update the Value for a Key in a Map in Java language with well detailed examples.