AbstractMap.SimpleEntry getKey() Method in Java with Examples - GeeksforGeeks

27 Jun, 2019
AbstractMap.SimpleEntry<K, V> is used to maintain a key and a value entry. The value can be changed using the setValue method. This class facilitates the process of building custom map implementations.getKey() method of AbstractMap.SimpleEntry<K, V> returns the key corresponding to this entry.
Syntax:
public K getKey()

Parameters: This method accepts nothing.
Return value: This method returns the key corresponding to this entry.
Below programs illustrate getKey() method:Program 1:
// Java program to demonstrate// AbstractMap.SimpleEntry.getKey() method  import java.util.*;import java.util.AbstractMap.SimpleEntry;  public class GFG {      @SuppressWarnings({ "unchecked", "rawtypes" })    public static void main(String[] args)    {          // create a ArrayList of Map        ArrayList<AbstractMap                      .SimpleEntry<Integer, Integer> >            arrayList            = new ArrayList<AbstractMap                                .SimpleEntry<Integer, Integer> >();          // add values        arrayList.add(new AbstractMap.SimpleEntry(0, 123));        arrayList.add(new AbstractMap.SimpleEntry(1, 130));        arrayList.add(new AbstractMap.SimpleEntry(2, 994));          // print keys        for (int i = 0; i < arrayList.size(); i++) {              // get map from list            AbstractMap.SimpleEntry<Integer, Integer>                map                = arrayList.get(i);              // get key from map.getKey();            int key = map.getKey();              System.out.println("Key " + key);        }    }}
Key 0
Key 1
Key 2

Program 2:
// Java program to demonstrate// AbstractMap.SimpleEntry.get() method  import java.util.*;  public class GFG {      @SuppressWarnings({ "unchecked", "rawtypes" })    public static void main(String[] args)    {          // create a ArrayList of Map        ArrayList<AbstractMap                      .SimpleEntry<String, String> >            arrayList            = new ArrayList<AbstractMap                                .SimpleEntry<String, String> >();          // add values        arrayList.add(new AbstractMap                          .SimpleEntry(" 001AB ", " Emp 1"));        arrayList.add(new AbstractMap                          .SimpleEntry(" 011AC ", " Emp 2"));        arrayList.add(new AbstractMap                          .SimpleEntry(" 111AD ", " Emp 3"));        arrayList.add(new AbstractMap                          .SimpleEntry(" 101BE ", " Emp 4"));        arrayList.add(new AbstractMap                          .SimpleEntry(" 110CE ", " Emp 5"));          // print keys        for (int i = 0; i < arrayList.size(); i++) {              // get map from list            AbstractMap.SimpleEntry<String, String>                map                = arrayList.get(i);              // get key from map.getKey()            String key = map.getKey();              System.out.println("Key " + key);        }    }}
Key  001AB 
Key  011AC 
Key  111AD 
Key  101BE 
Key  110CE

References: https://docs.oracle.com/javase/10/docs/api/java/util/AbstractMap.SimpleEntry.html#getKey()
Java - util package
Java-AbstractMap-SimpleEntry
Java-Functions
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Exceptions in Java
Constructors in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
PriorityQueue in Java
How to remove an element from ArrayList in Java?