TreeMap firstEntry() and firstKey() Method in Java with Examples
There are two variants of first() in Java.util.TreeMap, both are discussed in this article.
Method 1: firstEntry()
It returns a key-value mapping associated with the least key in this map, or null if the map is empty.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public Map.Entry firstEntry()
Return Type: An entry with the least key and null if the map is empty.
Lowest entry is: 1=one
Method 2: firstKey()
It returns the first (lowest) key currently in the map.
public K firstKey()
Return Type: The first (lowest) key currently in this map.
Exception Thrown: NoSuchElementException is thrown if this map is empty.
Lowest key is: 1
Implementation: These functions can be used to fetch the best-ranked person in the given list, or can be used to assign a winner in which person with the lowest time to finish a task wins. The latter one is discussed below.
Example: Practical Application
Winner with lowest time is : 1.3=Shambhavi
This article is contributed by Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.