TreeMap firstEntry() method in java

Let’s learn TreeMap firstEntry() method in java.

TreeMap firstEntry() method in java

firstEntry() method of TreeMap class returns a key-value mapping associated with the least key in this map, or null if the map is empty.


public Map.Entry<K, V> firstEntry()


firstEntry() method do not accept any parameters.


an entry with the least key, or null if this map is empty.

Now let’s see example on TreeMap firstEntry() method.

import java.util.TreeMap;
public class TreeMapFirstEntryMethodExample
   public static void main(String[] args)
      TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
      tm.put(69, "mango");
      tm.put(53, "apple");
      tm.put(26, "orange");
      tm.put(38, "grapes");
      tm.put(50, "banana");
      System.out.println("Checking TreeMap for first entry: ");
      System.out.println("TreeMap firstEntry is: " + tm.firstEntry());


Checking TreeMap for first entry:
TreeMap firstEntry is: 26=orange

Also read – major features of java