JavaTuples getKey() method

The getKey() method in org.javatuples is used to fetch the key from the TupleClassObject from the KeyValue Class. This method can be used with only KeyValue class object of javatuples library. It returns a Key which is the element present at the index 0 of the KeyValueClassObject. The returned Key is ensures the type-safety.

Method Declaration:

public A getKey()

Syntax:



KeyValue<A, B> KeyValueClassObject = KeyValue.with(A a, B b);
A val = KeyValueClassObject.getKey()

Return Value: This method returns a Key which is the element present at the index 0 of the KeyValueClassObject.

Below programs illustrate the various ways to use getKey() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is a Java program to get
// a KeyValue value
  
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),
                            "GeeksforGeeks");
  
        // Using getKey() method
        int key = kv.getKey();
  
        // Printing the Key
        System.out.println(key);
    }
}

chevron_right


Output:

1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is a Java program to get
// a KeyValue value
  
import java.util.*;
import org.javatuples.KeyValue;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating a KeyValue object
        KeyValue<String, String> kv
            = KeyValue.with("GeeksforGeeks",
                            "A Computer Science Portal for Geeks");
  
        // Using getKey() method
        String key = kv.getKey();
  
        // Printing the Key
        System.out.println(key);
    }
}

chevron_right


Output:

GeeksforGeeks


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.