Web / Apache Commons Collections Interview questions
What is a ReferenceMap in Apache Commons Collections?
ReferenceMap<K,V> is a Map whose keys and/or values are held through java.lang.ref soft or weak references rather than ordinary strong references.
ReferenceMap<String, byte[]> cache = new ReferenceMap<>(ReferenceStrength.HARD, ReferenceStrength.SOFT); cache.put("payload", largeByteArray); // entry may be reclaimed automatically if the JVM needs memory
Because the garbage collector can reclaim soft references under memory pressure (or weak references as soon as nothing else holds them), entries can disappear from a ReferenceMap on their own - which is a deliberate trade-off, not a bug.
This makes it well suited to caches or lookup tables where you'd rather silently lose stale entries than risk an OutOfMemoryError from a plain HashMap that never shrinks.
More Related questions...