🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the TreeMap firstKey() method in Java programming and how to use it with an example.
1. TreeMap firstKey() Method Overview
Definition:
The firstKey() method of the TreeMap class in Java is used to retrieve the first (lowest) key currently in this sorted map. TreeMap is sorted according to the natural ordering of its keys or by a comparator provided at map creation time.
Syntax:
public K firstKey()
Parameters:
- The method does not take any parameters.
Key Points:
- The firstKey() method returns the first (lowest) key currently in the TreeMap.
- If the map is empty, calling firstKey() will throw a NoSuchElementException.
- The method provides a means to access the keys in ascending order.
2. TreeMap firstKey() Method Example
import java.util.TreeMap;
public class TreeMapFirstKeyExample {
public static void main(String[] args) {
TreeMap<Integer, String> treeMap = new TreeMap<>();
// Putting some key-value pairs in the TreeMap
treeMap.put(3, "Three");
treeMap.put(1, "One");
treeMap.put(2, "Two");
// Printing the original TreeMap
System.out.println("Original TreeMap: " + treeMap);
// Using firstKey() to retrieve the first key in the TreeMap
Integer firstKey = treeMap.firstKey();
System.out.println("First Key: " + firstKey);
// Removing the first key and retrieving the new first key
treeMap.remove(firstKey);
Integer newFirstKey = treeMap.firstKey();
System.out.println("New First Key after removal: " + newFirstKey);
// Printing the updated TreeMap
System.out.println("Updated TreeMap: " + treeMap);
}
}
Output:
Original TreeMap: {1=One, 2=Two, 3=Three}
First Key: 1
New First Key after removal: 2
Updated TreeMap: {2=Two, 3=Three}
Explanation:
In this example, a TreeMap is created and populated with some key-value pairs.
The firstKey() method is then used to retrieve the first key in the map. The retrieved key is then removed from the map, and firstKey() is called again to find the new first key in the updated TreeMap.
The output shows the original TreeMap, the retrieved first keys, and the TreeMap after the removal of the first key.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment