TreeMap lastEntry() method in java code example

Example 1: TreeMap lastKey() method in java

import java.util.TreeMap;
public class TreeMapLastKeyMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
      tm.put(99, "yellow");
      tm.put(86, "violet");
      tm.put(93, "red");
      tm.put(36, "green");
      tm.put(29, "blue");
      System.out.println("Given TreeMap is: " + tm);
      // display lastKey of TreeMap
      System.out.println("last key is: " + tm.lastKey());
   }
}

Example 2: TreeMap lastKey() method in java

map integer values to string keys
import java.util.TreeMap;
public class TreeMapLastKeyMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
      tm.put("yellow", 99);
      tm.put("violet", 86);
      tm.put("red", 93);
      tm.put("green", 36);
      tm.put("blue", 29);
      System.out.println("Given TreeMap is: " + tm);
      // display lastKey of TreeMap
      System.out.println("last key is: " + tm.lastKey());
   }
}

Example 3: TreeMap lastEntry() method in java

import java.util.TreeMap;
public class TreeMapLastEntryMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
      tm.put(96, "mango");
      tm.put(39, "grapes");
      tm.put(56, "pineapple");
      tm.put(93, "apple");
      tm.put(69, "watermelon");
      // get value with greatest key
      System.out.println("Check last entry in given TreeMap: ");
      System.out.println("Value is: "+ tm.lastEntry());
   }
}

Tags:

Misc Example