Generated by GPT-5-mini| LinkedHashMap | |
|---|---|
| Name | LinkedHashMap |
| Type | Data structure |
| Introduced | 1996 |
| Language | Java |
| Package | java.util |
LinkedHashMap is a concrete collection class in Java that combines a hash table with a doubly linked list to maintain a predictable iteration order. It is part of the Java Platform, Standard Edition libraries and is widely used in applications requiring insertion-ordered or access-ordered maps. Implemented as a specialization of HashMap in the Java Development Kit, it is frequently discussed alongside ArrayList, HashSet, TreeMap, and ConcurrentHashMap in literature on Java Collections Framework, Sun Microsystems, and Oracle Corporation platform evolution.
LinkedHashMap preserves a specific iteration order by linking entries in a doubly linked list while using hashing for key lookup. The class sits within the java.util package and interacts with interfaces such as Map (Java), Serializable, and Cloneable. Its semantics and API are documented in the Java SE API Specification and have been examined in textbooks by authors like Herbert Schildt, Joshua Bloch, and Kathy Sierra. LinkedHashMap has been employed in frameworks and libraries developed by organizations like Apache Software Foundation, Spring Framework, and Google for predictable map traversal.
The internal representation couples a hash table bucket array derived from HashMap (Java) implementation with a doubly linked list that threads through the entries to record insertion or access sequence. Each entry is a node object similar to HashMap.Node and contains references for key, value, next-in-bucket, before, and after pointers. The class supports null keys and null values in the same manner as HashMap (Java), and it relies on Object.hashCode and Object.equals implementations of keys for correct behavior. Concurrency characteristics mirror those of HashMap: it is not thread-safe and requires external synchronization when accessed by multiple threads, as in usage with Collections.synchronizedMap or higher-level constructs like ConcurrentHashMap or CopyOnWriteArrayList.
Constructors include a default no-argument constructor, one accepting an initial capacity and load factor as in HashMap (Java), and a constructor that accepts another Map (Java) to create a new insertion-ordered map containing the mappings of the given map. Common methods mirror the Map (Java) interface: put, get, remove, containsKey, containsValue, size, isEmpty, clear, keySet, values, and entrySet. Methods such as putIfAbsent and computeIfAbsent are available when used through the Map default method implementations introduced in Java SE 8 alongside functional interfaces like java.util.function.Function and java.util.function.BiFunction. Clone and serialization behavior are specified by Cloneable and Serializable conventions found in the Java Language Specification.
LinkedHashMap supports two primary ordering modes: insertion-order and access-order. In insertion-order mode, entries iterate in the order in which keys were originally inserted, a behavior useful in deterministic output generation used by systems developed at Netflix, Twitter, or Facebook. In access-order mode, a get or put operation moves an entry to the end of the list, enabling LRU (least-recently-used) eviction strategies employed in caching solutions such as those implemented by Guava caches, Ehcache, and Caffeine (software). The removeEldestEntry method is provided to customize removal policy; it is often overridden in implementations found in Android (operating system) image caches or server-side session stores used in Apache Tomcat and Jetty (web server). Iteration order guarantees make LinkedHashMap preferable to TreeMap when natural ordering by keys is not desired.
Lookup, insertion, and deletion operations typically run in constant time O(1) under average conditions due to hash table indexing, similar to HashMap and HashSet. The added cost of maintaining the doubly linked list introduces a small constant overhead for insertions and deletions compared with plain hash-based maps. In worst-case scenarios—such as many colliding keys produced by poor Object.hashCode implementations or adversarial input—operations can degrade toward O(n), a risk also discussed in contexts like Denial-of-service attacks against hash tables and mitigated in later Java SE releases using balanced tree bins as in HashMap (Java 8) improvements. Memory footprint is higher than that of HashMap due to the additional per-entry pointers, which factors into capacity planning in large-scale systems operated by companies such as Amazon (company), Microsoft, and IBM.
LinkedHashMap is commonly used for predictable iteration in configuration processing, deterministic serialization for protocols handled by Apache Kafka or RabbitMQ, and building simple caches with predictable eviction in Spring Framework applications, Hibernate, and JVM services. Typical patterns include wrapping LinkedHashMap to implement an LRU cache via overriding removeEldestEntry, constructing insertion-ordered maps when reading properties files from GNU tools or Microsoft Windows registries, and using access-order maps in user-session least-recently-used eviction for web applications deployed on Google Cloud Platform or Amazon Web Services. Developers often reference guides from Oracle Corporation and community tutorials by Stack Overflow authors when implementing these patterns.
Category:Java collections