Saturday, September 6, 2025
HomeLanguagesJavaJavaTuples getKey() method

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:




// 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),
                            "Lazyroar");
  
        // Using getKey() method
        int key = kv.getKey();
  
        // Printing the Key
        System.out.println(key);
    }
}


Output:

1

Example 2:




// 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("Lazyroar",
                            "A Computer Science Portal for Geeks");
  
        // Using getKey() method
        String key = kv.getKey();
  
        // Printing the Key
        System.out.println(key);
    }
}


Output:

Lazyroar
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS