The TreeMap.firstKey()
method in Java is used to retrieve the first (lowest) key in the map. We will also cover a real-world use case to show how TreeMap.firstKey()
can be used effectively.
Table of Contents
- Introduction
firstKey
Method Syntax- Examples
- Retrieving the First Key from a TreeMap
- Handling an Empty TreeMap
- Real-World Use Case
- Example: Finding the First Student in a Class List
- Conclusion
Introduction
The TreeMap.firstKey()
method is a member of the TreeMap
class in Java. It allows you to retrieve the first (lowest) key in the map. If the map is empty, the method throws a NoSuchElementException
.
firstKey() Method Syntax
The syntax for the firstKey
method is as follows:
public K firstKey()
- The method does not take any parameters.
- The method returns the first (lowest) key in the map.
Examples
Retrieving the First Key from a TreeMap
The firstKey
method can be used to get the first key in the TreeMap
.
Example
import java.util.TreeMap;
public class FirstKeyExample {
public static void main(String[] args) {
// Creating a TreeMap with String keys and Integer values
TreeMap<String, Integer> treeMap = new TreeMap<>();
// Adding entries to the TreeMap
treeMap.put("Ravi", 25);
treeMap.put("Priya", 30);
treeMap.put("Vijay", 35);
treeMap.put("Anita", 28);
treeMap.put("Suresh", 40);
// Retrieving the first key from the TreeMap
String firstKey = treeMap.firstKey();
// Printing the first key
System.out.println("First key: " + firstKey);
}
}
Output:
First key: Anita
Handling an Empty TreeMap
The firstKey
method throws a NoSuchElementException
if the TreeMap
is empty.
Example
import java.util.TreeMap;
public class EmptyTreeMapExample {
public static void main(String[] args) {
// Creating an empty TreeMap
TreeMap<String, Integer> treeMap = new TreeMap<>();
try {
// Attempting to retrieve the first key from an empty TreeMap
String firstKey = treeMap.firstKey();
System.out.println("First key in empty TreeMap: " + firstKey);
} catch (java.util.NoSuchElementException e) {
System.out.println("The TreeMap is empty.");
}
}
}
Output:
The TreeMap is empty.
Real-World Use Case
Example: Finding the First Student in a Class List
A common real-world use case for TreeMap.firstKey()
is finding the first student in a class list where students are sorted by their names.
Example
import java.util.TreeMap;
public class StudentManager {
public static void main(String[] args) {
// Creating a TreeMap to manage student names and their IDs
TreeMap<String, Integer> students = new TreeMap<>();
// Adding student names and their IDs to the TreeMap
students.put("Ravi", 101);
students.put("Priya", 102);
students.put("Vijay", 103);
students.put("Anita", 104);
students.put("Suresh", 105);
// Retrieving the first student's name in the class list
String firstStudent = students.firstKey();
// Printing the first student's name
System.out.println("First student in the class list: " + firstStudent);
}
}
Output:
First student in the class list: Anita
In this example, TreeMap.firstKey()
is used to find the first student’s name in a class list based on their names, making it easy to identify and work with the first student in the list.
Conclusion
The TreeMap.firstKey()
method in Java provides a way to retrieve the first (lowest) key in the map. By understanding how to use this method, you can efficiently manage and navigate collections of key-value pairs in your Java applications. The method allows you to find and work with the lowest keys in the map, making it a versatile tool for data management in various scenarios.