In this shot, we will discuss how to use the SortedMap.lastKey()
method in Java. The SortedMap.lastKey()
method is present in the SortedMap
interface inside the java.util
package.
The SortedMap.lastKey()
method is used to obtain the greatest key-element present in the SortedMap
.
The SortedMap.lastKey()
method doesn’t take any parameters.
The SortedMap.lastKey()
method returns:
SortedMap
.Let’s look at the code snippet below.
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(30,"java");System.out.println("SortedMap mappings are: "+ s);System.out.println("The last key in the SortedMap is: " + s.lastKey());}}
Main
class.main
function.SortedMap
consisting of integer type keys and string type values.TreeMap.put()
method.SortedMap
.SortedMap.lastKey()
method to obtain the greatest key of SortedMap
and display it.In this way, we can use the SortedMap.lastKey()
method in Java.