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
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)