In this shot, we will discuss how to use the SortedMap.keySet() method in Java.
SortedMap.keySet() methods are present in the SortedMap interface inside the java.util package.
The SortedMap.keySet() method is used to create a new set and store the key elements of the TreeMap in sorted order.
The SortedMap.keySet() method doesn’t take any parameter.
The SortedMap.keySet() method returns:
Set: A set with the same keys as that of the map in increasing order.Let’s look at the below code snippet.
import java.util.*;class Main{public static void main(String[] args){SortedMap<Integer, String> s = new TreeMap<Integer,String>();s.put(2,"Welcome");s.put(12,"to");s.put(31,"the");s.put(18,"world");s.put(25,"of");s.put(36,"java");System.out.println("SortedMap mappings are: "+ s);System.out.println("The set of keys in sorted order using keySet() method is:" + s.keySet());}}
Main class.main function.SortedMap consisting of Integer type keys and String type values.TreeMap.put() method.SortedMap.SortedMap.keySet() method to obtain the set with the same keys in the sorted order as SortedMap and displayed it.In this way, we can use the SortedMap.keySet() method in Java.