Get first or last entry of a LinkedHashMap
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
LinkedHashMap is a widely used collection in Java that combines the uniqueness of keys, similar to a HashMap, with the ordering of entries, similar to a List. This data structure maintains a doubly-linked list through its entries, allowing it to remember the insertion order. This characteristic makes it particularly useful when you need a deterministic iteration order.
In this article, we'll explore techniques to get the first or last entry from a LinkedHashMap, understand its underlying mechanisms, and delve into the nuances that differentiate it from other maps.
Key Characteristics of LinkedHashMap
- Maintains Order: Unlike
HashMap,LinkedHashMappreserves the order of inserted elements. - Fast Iteration: Due to its doubly-linked list structure, iterating over a
LinkedHashMapis generally more predictable. - Performance: Offers O(1) performance for basic operations like add, remove, and contains as it extends
HashMap.
Retrieve the First Entry
Using an Iterator
To obtain the first entry of a LinkedHashMap, an iterator can be used on the entry set. Here's how you can accomplish that:
Explanation
The method entrySet().iterator().next() acquires an iterator from the keys' ordered set and fetches the first element without removing it. It's efficient as it leverages the inherent order of LinkedHashMap.
Retrieve the Last Entry
Using an Iterator
Similar to retrieving the first entry, the last entry requires iterating through the map. However, given the iterator's nature, it requires us to traverse the entire map:
Explanation
Though it requires traversing the map to reach the last entry, this solution still maintains efficiency due to LinkedHashMap's ordered nature.
Summary Table
Here's a quick reference comparing LinkedHashMap operations:
| Operation | Method | Time Complexity | Explanation |
| Get First Entry | entrySet().iterator().next() | O(1) | Directly retrieves the first element |
| Get Last Entry | Iterate to the last | O(n) | Traverses to fetch the last element |
| Insertion | put() | O(1) | Similar to HashMap insertion |
| Search by Key | containsKey() | O(1) | Efficient look-up by key |
Technical Considerations
- Synchronized Access:
LinkedHashMapis not synchronized. If concurrent access is needed, consider wrapping it withCollections.synchronizedMap(new LinkedHashMap<>());. - Memory: Memory consumption can be slightly higher than
HashMapdue to the additional linked list pointers. - Removal Order: Besides maintaining insertion order,
LinkedHashMapcan be configured to maintain access order, which affects how elements are iterated after accessing.
Conclusion
The LinkedHashMap is a versatile map offering predictable iteration, akin to a list, while benefiting from the hashed map operations. While getting the first entry is straightforward, obtaining the last entry involves iterating through the collection. Understanding these methods can optimize data structure utilization, especially in scenarios demanding ordered entry access.
With this understanding, developers can harness LinkedHashMap's features effectively to manage collections that require consistent ordering, leading to more reliable and maintainable codebases.
Related reading
- Get generic type of java.util.List
- Get JavaScript object from array of objects by value of property
- Get key by value in dictionary
- Get key by value in dictionary
- Get integer value of the current year in Java
- Get java.nio.file.Path object from java.io.File
- Get keys from HashMap in Java
- Get last element of Stream/List in a one-liner

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.