Web / Apache Commons Collections Interview questions
What is an LRUMap in Apache Commons Collections?
LRUMap<K,V> is a bounded Map that automatically evicts its Least Recently Used entry once a configured maximum size is reached and a new entry needs to be inserted.
LRUMap<String, String> cache = new LRUMap<>(2); cache.put("a", "1"); cache.put("b", "2"); cache.get("a"); // "a" is now most recently used cache.put("c", "3"); // "b" gets evicted, not "a"
Every get() or put() refreshes an entry's recency, so the entry that hasn't been touched the longest is the one removed - making LRUMap a simple in-memory cache when you want a hard size cap without wiring up a full caching library.
More Related questions...