Open In App

TreeMap firstEntry() and firstKey() Method in Java with Examples

Last Updated : 20 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
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





Output: 

Lowest entry is: 1=one

Method 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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads