JavaTuples getKey() method
Last Updated :
27 Aug, 2018
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:
import java.util.*;
import org.javatuples.KeyValue;
class GfG {
public static void main(String[] args)
{
KeyValue<Integer, String> kv
= KeyValue.with(Integer.valueOf( 1 ),
"GeeksforGeeks" );
int key = kv.getKey();
System.out.println(key);
}
}
|
Output:
1
Example 2:
import java.util.*;
import org.javatuples.KeyValue;
class GfG {
public static void main(String[] args)
{
KeyValue<String, String> kv
= KeyValue.with( "GeeksforGeeks" ,
"A Computer Science Portal for Geeks" );
String key = kv.getKey();
System.out.println(key);
}
}
|
Output:
GeeksforGeeks
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...