JavaTuples setKey()方法

这个 setKey() 方法 组织。Java元组 用于设置KeyValue类对象的键。该方法只能用于javatuples库的KeyValue类对象。它返回另一个KeyValueClassObject,其中键是作为参数传递的元素,值来自前一个KeyValueClassObject。

null

方法声明:

public <X> KeyValue<X, B> setKey(X key)

语法:

KeyValue<X, B> KeyValueClassObject = KeyValue.setKey(X key)

返回值: 此方法返回另一个KeyValueClassObject,其中键作为作为参数传递的元素,值来自前一个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<Integer, String> kv
= KeyValue.with(Integer.valueOf( 1 ),
"A computer science portal" );
// Using setKey() method
KeyValue<String, String> 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<Integer, String> kv
= KeyValue.with(Integer.valueOf( 1 ),
"1" );
// Using setKey() method
KeyValue<String, String> kv1 = kv.setKey( "One" );
// Printing the returned KeyValue
System.out.println(kv1);
}
}


输出 :

[One, 1]
© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享