Sunday, January 25, 2026
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

1 COMMENT

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12065 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7221 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS