JavaTuples setKey() method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

[GeeksforGeeks, A computer science portal]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

[One, 1]


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.