Java.util.TreeMap.firstEntry() and firstKey() in Java

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the working
// of firstKey()
import java.io.*;
import java.util.*;
public class firstKey {
public static void main(String[] args)
    {
  
        // Declaring the tree map of Integer and String
        TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
  
        // assigning the values in the tree map
        // using put()
        treemap.put(2, "two");
        treemap.put(7, "seven");
        treemap.put(3, "three");
        treemap.put(1, "one");
        treemap.put(6, "six");
        treemap.put(9, "nine");
  
        // use of firstEntry()
        System.out.println("Lowest entry is: " + treemap.firstEntry());
    }
}

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the working
// of firstKey()
import java.io.*;
import java.util.*;
public class firstKey {
public static void main(String[] args)
    {
  
        // Declaring the tree map of Integer and String
        TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
  
        // assigning the values in the tree map
        // using put()
        treemap.put(2, "two");
        treemap.put(1, "one");
        treemap.put(3, "three");
        treemap.put(6, "six");
        treemap.put(5, "five");
        treemap.put(9, "nine");
  
        // Use of firstKey()
        System.out.println("Lowest key is: " + treemap.firstKey());
    }
}

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the application
// of firstKey() and firstEntry()
import java.io.*;
import java.util.*;
public class FirstAppli {
public static void main(String[] args)
    {
  
        // Declaring the tree map of Integer and String
        // times of participants (in seconds)
        TreeMap<Float, String> time = new TreeMap<Float, String>();
  
        // assigning the time taken to complete task
        // using put()
        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");
  
        // use of firstEntry()
        // printing person with least time
        System.out.println("Winner with lowest time is : " + time.firstEntry());
    }
}

chevron_right


Output:

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 contribute@geeksforgeeks.org. 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.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.