TreeMap lastEntry() Method in Java with Examples
Last Updated :
02 Sep, 2020
The java.util.TreeMap.lastEntry() method is used to returns the key-value mapping associated with the greatest key in this map, or null if the map is empty.
Syntax:
tree_map.lastEntry()
Parameters: The method does not take any parameters.
Return Value: The method returns an entry with the greatest key, or null if this map is empty.
Below examples illustrates the working of java.util.TreeMap.lastEntry() method:
Example 1: When the map is not empty.
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
TreeMap<Integer, String> tree_map
= new TreeMap<Integer, String>();
tree_map.put( 98 , "Geeks" );
tree_map.put( 100 , "For" );
tree_map.put( 103 , "Geeks" );
System.out.println( "The last entry is : "
+ tree_map.lastEntry());
}
}
|
Output
The last entry is : 103=Geeks
Example 2: When the map is empty.
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
TreeMap<Integer, String> tree_map
= new TreeMap<Integer, String>();
System.out.println( "The last entry is : "
+ tree_map.lastEntry());
}
}
|
Output
The last entry is : null
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...