Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the TreeMap.firstEntry() method in Java?

Harsh Jain

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Introduction

The TreeMap.firstEntry() method is used to obtain the key-value mapping associated with the lowest key in the map.

The TreeMap.firstEntry() method is present in the TreeMap class inside the java.util package.

Syntax

The syntax of the TreeMap.firstEntry() method is given below:

Map.entry firstEntry() 

Parameters TreeMap.firstEntry() does not accept any parameters.

Return value

The TreeMap.firstEntry() method returns one value:

  • Entry: The key-value pair associated with the lowest key in the map.

Code

Let’s have a look at the code.

import java.util.*;
class Main
{
public static void main(String[] args)
{
TreeMap<Integer, String> t1 = new TreeMap<Integer, String>();
t1.put(1, "Let's");
t1.put(5, "see");
t1.put(2, "TreeMap class");
t1.put(27, "methods");
t1.put(9, "in java.");
System.out.println("The key-value mapping associated with " +
"the lowest key in the map is: " + t1.firstEntry());
TreeMap<String, Integer> t2 = new TreeMap<String, Integer>();
t2.put("ab", 5);
t2.put("baa", 1);
t2.put("cbc", 2);
t2.put("d", 27);
t2.put("e", 9);
System.out.println("The key-value mapping associated with " +
"the lowest key in the map is: " + t2.firstEntry());
}
}
Using the TreeMap.firstEntry() method in Java

Explanation

  • In line 1, we import the required package.

  • In line 2, we make a Main class.

  • In line 4, we make a main() function.

  • In line 6, we declare a TreeMap that consists of keys of type Integerand values of type String.

  • In lines 8 to 12, we use the TreeMap.put() method to insert values in the TreeMap.

  • In line 14, we use the TreeMap.firstEntry() method and display the key-value pair associated with the lowest key in the map with a message.

  • In lines 17 to 26, we create another TreeMap object that contains keys of type String and values of type Integer. Then, we can see from the output that the lowest key in the case of strings would be considered based on alphabetic order.

So, this is the way to use the TreeMap.firstEntry() method in Java.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring