LinkedHashMap-backed LRU cache with size bound

Kotlin
class LruCache<K, V>(private val capacity: Int) : LinkedHashMap<K, V>(capacity, 0.75f, true) {
    init {
        require(capacity > 0) { "capacity must be positive" }
    }

    override fun removeEldestEntry(eldest: MutableMap.MutableEntry<K, V>?): Boolean =
        size > capacity
}

val cache = LruCache<String, Int>(2)
cache["a"] = 1
cache["b"] = 2
cache["a"] // touch a, so b becomes the eviction candidate
cache["c"] = 3
check(cache.keys == setOf("a", "c"))