There are two variants of first() in Java.util.TreeMap, both are discussed in this article.
1. firstEntry() : It returns a key-value mapping associated with the least key in this map, or null if the map is empty.
Syntax : public Map.Entry firstEntry() Parameters: NA Return Value: It returns an entry with the least key and null if the map is empty. Exception: NA
Lowest entry is: 1=one
2. firstKey() : It returns the first (lowest) key currently in the map.
Syntax : public K firstKey() Parameters: NA Return Value: It returns the first (lowest) key currently in this map. Exception: NA NoSuchElementException : It is thrown if this map is empty.
Lowest key is: 1
Practical Application : These functions can be used to fetch best ranked person in given list, or can be used to assign winner in game in which person with lowest time to finish a task wins. Latter one is discussed below.
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 contribute.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.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- SortedMap firstKey() method in Java
- Java.util.TreeMap.descendingMap() and descendingKeyset() in Java
- Java.util.TreeMap.containskey() and containsValue() in Java
- Java.util.TreeMap.pollFirstEntry() and pollLastEntry() in Java
- Java.util.TreeMap.put() and putAll() in Java
- Java.util.TreeMap.floorEntry() and floorKey() in Java
- Java Swing | Translucent and shaped Window in Java
- Difference between Core Java and Advanced Java
- Difference between a Java Application and a Java Applet
- Difference between Java IO and Java NIO
- Difference between Java and Core Java
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java
- Java.io.BufferedWriter class methods in Java
- Java.io.DataOutputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 1