Skip to main content
PreparationZone Logo
PreparationZone
Back to Java

Java · Hard · Pro

LinkedHashMap access-order for LRU cache — removeEldestEntry?

JavaCollectionsSenior
Model answerPreview

LRU with LinkedHashMap

LinkedHashMap with accessOrder=true moves entries on get/put, maintaining insertion or access order. Override removeEldestEntry to evict the eldest when size exceeds capacity—classic LRU cache in ~20 lines.