AbstractMap.SimpleEntry setValue(V value) Method in Java with Examples

AbstractMap.SimpleEntry<K, V> is used to maintain a key and a value entry. The value can be changed using the setValue method. This class facilitates the process of building custom map implementations.
setValue(V value) method of AbstractMap.SimpleEntry<K, V> used to replace the current value of map with the specified value passed as parameter.
Syntax:
public V setValue(V value)
Parameters: This method accepts the value we want to set.
Return value: This method return the old value corresponding to the entry.
Below programs illustrate setValue(V value) method:
Program 1:
// Java program to demonstrate// AbstractMap.SimpleEntry.setValue() method import java.util.*; public class GFG { @SuppressWarnings({ "unchecked", "rawtypes" }) public static void main(String[] args) { AbstractMap.SimpleEntry<Integer, Integer> map = new AbstractMap .SimpleEntry(0, 123); // change value to 2122425 Integer newValue = 2122425; Integer oldValue = map.setValue(newValue); System.out.println("Value changed from " + oldValue + " to " + map.getValue()); }} |
Output:
Value changed from 123 to 2122425
Program 2:
// Java program to demonstrate// AbstractMap.SimpleEntry.setValue() method import java.util.*; public class GFG { @SuppressWarnings({ "unchecked", "rawtypes" }) public static void main(String[] args) { AbstractMap.SimpleEntry<String, String> map = new AbstractMap .SimpleEntry<String, String>("Captain:", "Dhoni"); // change value to Kohli String newValue = "Kohli"; String oldValue = map.setValue(newValue); System.out.println("Value changed from " + oldValue + " to " + map.getValue()); }} |
Output:
Value changed from Dhoni to Kohli
References: https://docs.oracle.com/javase/10/docs/api/java/util/AbstractMap.SimpleEntry.html#setValue(V)



