Thursday, October 9, 2025
HomeLanguagesJavaAbstractMap.SimpleEntry setValue(V value) Method in Java with Examples

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)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32346 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6790 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS