org.javatuples中的setKey()方法用于设置KeyValue Class对象的键。此方法只能与javatuples库的KeyValue类对象一起使用。它返回另一个KeyValueClassObject,其Key作为参数传递为元素,并且返回先前KeyValueClassObject的值。
方法声明:
public KeyValue setKey(X key)
用法:
KeyValue KeyValueClassObject = KeyValue.setKey(X key)
返回值:此方法返回另一个KeyValueClassObject,其Key作为参数传递为元素,并且返回先前KeyValueClassObject的值。
以下示例程序旨在说明使用setKey()方法的各种方法:
例子1:
// Below is a Java program to set
// key in a KeyValue pair
import java.util.*;
import org.javatuples.KeyValue;
class GfG {
public static void main(String[] args)
{
// Creating a KeyValue object
KeyValue kv
= KeyValue.with(Integer.valueOf(1),
"A computer science portal");
// Using setKey() method
KeyValue kv1 = kv.setKey("GeeksforGeeks");
// Printing the returned KeyValue
System.out.println(kv1);
}
}
输出:
[GeeksforGeeks, A computer science portal]
例子2:
// Below is a Java program to set
// key in a KeyValue pair
import java.util.*;
import org.javatuples.KeyValue;
class GfG {
public static void main(String[] args)
{
// Creating a KeyValue object
KeyValue kv
= KeyValue.with(Integer.valueOf(1),
"1");
// Using setKey() method
KeyValue kv1 = kv.setKey("One");
// Printing the returned KeyValue
System.out.println(kv1);
}
}
输出:
[One, 1]