Java中的 AbstractMap.SimpleEntry setValue(V value) 方法及示例
AbstractMap.SimpleEntry
AbstractMap.SimpleEntry
句法:
public V setValue(V value)
参数:此方法接受我们要设置的值。
返回值:该方法返回条目对应的旧值。
下面的程序说明了 setValue(V value) 方法:
方案一:
// 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
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());
}
}
输出:
Value changed from 123 to 2122425
方案二:
// 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 map
= new AbstractMap
.SimpleEntry("Captain:", "Dhoni");
// change value to Kohli
String newValue = "Kohli";
String oldValue = map.setValue(newValue);
System.out.println("Value changed from " + oldValue
+ " to " + map.getValue());
}
}
输出:
Value changed from Dhoni to Kohli
参考资料: https: Java/util/AbstractMap.SimpleEntry.html#setValue(V)