🎓 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
1. Introduction
Java provides various types of Map implementations that are suitable for different scenarios. TreeMap and LinkedHashMap are two such implementations. TreeMap stores its entries in a sorted order, determined by the natural ordering of its keys, or by a Comparator provided at map creation time. LinkedHashMap, meanwhile, maintains insertion order, or optionally, access order of the elements.
2. Key Points
1. TreeMap is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation.
2. LinkedHashMap maintains a doubly-linked list running through all its entries, which defines the iteration ordering.
3. TreeMap has O(log n) time cost for the containsKey, get, put, and remove operations.
4. LinkedHashMap has O(1) time cost for the insertion, deletion, and access operations.
5. LinkedHashMap can be constructed to follow the order of insertion or the order in which entries were last accessed.
3. Differences
| TreeMap | LinkedHashMap |
|---|---|
| Sorts keys by natural ordering or by a comparator. | Maintains keys in insertion order or last access order. |
| Has O(log n) time complexity for insertion, deletion, and searching. | Has O(1) time complexity for insertion, deletion, and searching. |
| No guarantees on iteration order in terms of insertion or access. | Iteration order is predictable (insertion order by default). |
4. Example
// Import the necessary classes
import java.util.TreeMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class MapComparison {
public static void main(String[] args) {
// Step 1: Create a TreeMap
Map<Integer, String> treeMap = new TreeMap<>();
// Step 2: Create a LinkedHashMap
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
// Step 3: Add some entries to the TreeMap
treeMap.put(3, "Three");
treeMap.put(1, "One");
treeMap.put(2, "Two");
// Step 4: Add some entries to the LinkedHashMap
linkedHashMap.put(3, "Three");
linkedHashMap.put(1, "One");
linkedHashMap.put(2, "Two");
// Step 5: Print the contents of both maps
System.out.println("TreeMap: " + treeMap);
System.out.println("LinkedHashMap: " + linkedHashMap);
}
}
Output:
TreeMap: {1=One, 2=Two, 3=Three}
LinkedHashMap: {3=Three, 1=One, 2=Two}
Explanation:
1. TreeMap and LinkedHashMap are created to store integers associated with strings.
2. Entries are added to both maps; however, TreeMap sorts them by the natural ordering of the keys.
3. LinkedHashMap maintains the insertion order of the keys.
4. The output displays the sorting behavior of TreeMap and the insertion-order maintenance of LinkedHashMap.
5. When to use?
- Use TreeMap when you need to maintain an order of the keys. It's beneficial when you're concerned with the sorted or navigable map operations.
- Use LinkedHashMap when you want to maintain the order of entries either by insertion or by access, and need quick insertion and access operations.
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