VOOZH about

URL: https://www.geeksforgeeks.org/java/treemap-firstentry-method-in-java-with-examples/

⇱ TreeMap firstEntry() Method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

TreeMap firstEntry() Method in Java with Examples

Last Updated : 25 Aug, 2020

TreeMap firstEntry() refers to the method used to retrieve the key-value pairs mapped with the lowest key value element that exists in the TreeMap, if there are no key-value pairs within the Map, simply returns null. The method name 'firstEntry()' explains itself that it will return entry having the least key element with its value.

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

Here K and V refer key-value respectively.

Parameters: NA - As it doesnot accept any parameter.

Return Value: It returns an entry with the least key (lowest key-value pair) and null if the 
TreeMap is empty.

Exception: NA - As it doesnot throw any exception at entry return.

Example 1:


Output
Lowest Entry is: 1=Python

By the above example, it's clear that firstEntry() checks compare each key of TreeMap, and returns having the least key-value pair.

Example 2:


Output
Lowest Entry is: 65.5=Gulshan

Some points on TreeMap firstEntry():

  • TreeMap firstEntry() is available in java.util package.
  • It does not throw an exception at return entry time.
  • TreeMap firstEntry() method is a non-static method as it is accessible by the class's object, other than that, will get an error.
Comment