A LinkedHashMap is a HashMap that maintains the insertion order (or access order if configured). It is part of the java.util package and combines hash table and linked list features.
đ Key Features
Feature
Description
Maintains Order
Keeps the insertion order (or access order)
Allows null keys/values
â Yes (only one null key, multiple null values)
Thread-safe
â No (use Collections.synchronizedMap() for safety)
Performance
Slightly slower than HashMap due to order maintenance
LinkedHashMap<Integer,String>cache=newLinkedHashMap<>(16,0.75f,true){protectedbooleanremoveEldestEntry(Map.Entry<Integer,String>eldest){returnsize()>3;// Keep max 3 items}};
đ Comparison with HashMap and TreeMap
Feature
HashMap
LinkedHashMap
TreeMap
Order Maintained
â No
â Yes (Insertion/Access)
â Yes (Sorted)
Performance
⥠Fast
⥠Slightly slower
đĸ Slower (sorted)
Underlying Structure
Hash Table
Hash Table + Linked List
Red-Black Tree
â When to Use LinkedHashMap
You need a Map with predictable insertion order
You want to implement LRU caching
You want fast access but also ordered output
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)