In this shot, we will discuss how to use the SortedMap.firstKey() method in Java.
SortedMap.firstKey() method are present in the SortedMap interface inside the java.util package.
The SortedMap.firstKey() method is used to obtain the lowest key-element present in the SortedMap.
The SortedMap.firstKey() method doesn’t take any parameter.
The SortedMap.firstKey() method returns:
SortedMap.Let us look at the below code snippet:
import java.util.*;class Main{public static void main(String[] args){// creating a TreeMap of SortedMapSortedMap<Integer, String> s = new TreeMap<Integer,String>();// inserting elements in the SortedMaps.put(2,"Welcome");s.put(12,"to");s.put(31,"the");s.put(18,"world");s.put(25,"of");s.put(36,"java");// displaying the whole mapSystem.out.println("SortedMap mappings are: "+ s);// displaying the first key element in the mapSystem.out.println("The first key in the SortedMap is: " + s.firstKey());}}
Main class.main function.SortedMap consisting of Integer type keys and String type values.TreeMap.put() method.SortedMap.SortedMap.firstKey() method to obtain the lowest key of SortedMap and displayed it.In this way, we can use SortedMap.firstKey() method in Java.