JavaTuples setKey() method
Last Updated :
27 Aug, 2018
The setKey() method in org.javatuples is used to set the key of the KeyValue Class object. This method can be used with only KeyValue class object of javatuples library. It returns another KeyValueClassObject with the Key as the element passed as the parameter, and the value from the previous KeyValueClassObject.
Method Declaration:
public <X> KeyValue<X, B> setKey(X key)
Syntax:
KeyValue<X, B> KeyValueClassObject = KeyValue.setKey(X key)
Return Value: This method returns another KeyValueClassObject with the Key as the element passed as the parameter, and the value from the previous KeyValueClassObject.
Below programs illustrate the various ways to use setKey() method:
Example 1:
import java.util.*;
import org.javatuples.KeyValue;
class GfG {
public static void main(String[] args)
{
KeyValue<Integer, String> kv
= KeyValue.with(Integer.valueOf( 1 ),
"A computer science portal" );
KeyValue<String, String> kv1 = kv.setKey( "GeeksforGeeks" );
System.out.println(kv1);
}
}
|
Output:
[GeeksforGeeks, A computer science portal]
Example 2:
import java.util.*;
import org.javatuples.KeyValue;
class GfG {
public static void main(String[] args)
{
KeyValue<Integer, String> kv
= KeyValue.with(Integer.valueOf( 1 ),
"1" );
KeyValue<String, String> kv1 = kv.setKey( "One" );
System.out.println(kv1);
}
}
|
Output:
[One, 1]
Share your thoughts in the comments
Please Login to comment...