TreeMap firstEntry() and firstKey() Method in Java with Examples Last Updated : 20 Nov, 2021 Comments Improve Suggest changes Like Article Like Report 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. Syntax: public Map.Entry firstEntry() Return Type: An entry with the least key and null if the map is empty. Example: Java // Java Program to Illustrate Working of firstKey() Method // of TreeMap class // Importing required classes import java.io.*; import java.util.*; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an empty TreeMap by // declaring object of integer, strings pairs TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); // Populating values in the TreeMap // using put() method treemap.put(2, "two"); treemap.put(7, "seven"); treemap.put(3, "three"); treemap.put(1, "one"); treemap.put(6, "six"); treemap.put(9, "nine"); // Printing the lowest entry in TreeMap by // using firstEntry() method System.out.println("Lowest entry is: " + treemap.firstEntry()); } } Output: Lowest entry is: 1=oneMethod 2: firstKey() It returns the first (lowest) key currently in the map. Syntax: public K firstKey() Return Type: The first (lowest) key currently in this map. Exception Thrown: NoSuchElementException is thrown if this map is empty. Example: Java // Java Program to Demonstrate firstKey() Method // of TreeMap class // Importing required classes import java.io.*; import java.util.*; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an empty TreeMap by // declaring object of integer, strings pairs TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); // Populating values in the TreeMap // using put() method treemap.put(2, "two"); treemap.put(1, "one"); treemap.put(3, "three"); treemap.put(6, "six"); treemap.put(5, "five"); treemap.put(9, "nine"); // Printing the lowest entry in TreeMap by // using firstKey() method System.out.println("Lowest key is: " + treemap.firstKey()); } } Output: 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 Java // Java Program to Demonstrate Application Usage // of firstKey() and firstEntry() Methods // of TreeMap class // Importing required classes import java.io.*; import java.util.*; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an empty TreeMap // of Integer and String times of participants // In seconds TreeMap<Float, String> time = new TreeMap<Float, String>(); // Populating the time taken to complete task // using put() method time.put(2.32f, "Astha"); time.put(7.43f, "Manjeet"); time.put(1.3f, "Shambhavi"); time.put(5.63f, "Nikhil"); time.put(6.26f, "Vaishnavi"); // Printing person with least time // using of firstEntry() method System.out.println("Winner with lowest time is : " + time.firstEntry()); } } Output: Winner with lowest time is : 1.3=Shambhavi Comment More infoAdvertise with us Next Article Java TreeMap get() Method S Shambhavi Singh Improve Article Tags : Java Technical Scripter Java - util package Practice Tags : Java Similar Reads TreeMap in Java TreeMap is a part of the Java Collection Framework. It implements the Map and NavigableMap interface and extends the AbstractMap class. It stores key-value pairs in a sorted order based on the natural ordering of keys or a custom Comparator. It uses a Red-Black Tree for efficient operations (add, re 11 min read Java TreeMap clear() Method The clear() method is a built-in method of the TreeMap class in Java of the java.util package. This method is used to remove all key-value mappings from the TreeMap. And after invoking this method, the map becomes completely empty.This method is very useful when we want to reset the map or discard a 2 min read TreeMap clone() Method in Java with Examples In Java, clone() method of the TreeMap class is used to return a shallow copy of the mentioned treemap. It just creates a copy of the map. --> java.util Package --> TreeMap Class --> clone() Method Syntax: Tree_Map.clone() Parameters: The method does not take any parameters. Return Type: A 2 min read Java TreeMap containsKey() Method The containsKey() method in Java is used to check if a given key exists in a TreeMap. If the key is present in the map, it returns true, otherwise, it returns false. This method is very helpful when we need to verify whether a mapping exists before performing any operation on it.Syntax of TreeMap co 3 min read TreeMap containsValue() Method in Java With Examples In Java, containsValue() method of the TreeMap class is used to check whether a particular value is being mapped by any key in the TreeMap. It takes the value as a parameter and returns True if that value is mapped by any of the keys in the map. --> java.util Package --> TreeMap class --> c 3 min read TreeMap entrySet() Method in Java The java.util.TreeMap.entrySet() method in Java is used to create a set out of the same elements contained in the treemap. It basically returns a set view of the treemap or we can create a new set and store the map elements into them. Syntax: tree_map.entrySet() Parameters: The method does not take 2 min read 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. Syntax: public Map.Entry firstEntry() Return Type: An entry with the least key and 3 min read Java TreeMap get() Method The get() method of the TreeMap class in Java is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. If the key does not exist in the map, the method returns null.Syntax of TreeMap get() MethodtreeMap.get(Object key)Parameter: key: The method takes one paramete 2 min read TreeMap headMap() Method in Java The java.util.TreeMap.headMap(key_point) method of TreeMap class is used to get all the pairs or portion of the map strictly less than the parameter key_value. The mentioned parameter is excluded from the newly prepared treemap. Since the set is backed by the map, so any changes to the map are refle 3 min read TreeMap keySet() Method in Java with Examples In Java, keySet() method of TreeMap class is present inside java.util package in Java is used to create a set out of the key elements contained in the treemap. It basically returns a set view of the keys or we can create a new set and store the key elements in them in ascending order. Since the set 3 min read Like