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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

Value changed from 123 to 2122425

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

Value changed from Dhoni to Kohli

References: https://docs.oracle.com/javase/10/docs/api/java/util/AbstractMap.SimpleEntry.html#setValue(V)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.