AbstractMap.SimpleEntry setValue(V value) Method in Java with Examples
Last Updated :
27 Jun, 2019
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:
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 );
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:
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" );
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)
Share your thoughts in the comments
Please Login to comment...