# LruCache

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  
Summary:[Ctors](https://developer.android.com/reference/android/support/v4/util/LruCache#pubctors)\|[Methods](https://developer.android.com/reference/android/support/v4/util/LruCache#pubmethods)\|[Protected Methods](https://developer.android.com/reference/android/support/v4/util/LruCache#promethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/v4/util/LruCache#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/v4/util/LruCache#)  

# LruCache

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public


class
LruCache
`  
`

extends Object


``


`

|---|------------------------------------------|
| java.lang.Object                            ||
| ↳ | android.support.v4.util.LruCache\<K, V\> |

<br />

*** ** * ** ***

Static library version of`android.util.LruCache`. Used to write apps that run on API levels prior to 12. When running on API level 12 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.

## Summary

|                                                 ### Public constructors                                                  ||
|-----------------------------------------------------------------------------------------------------------------------|---|
| ` `[LruCache](https://developer.android.com/reference/android/support/v4/util/LruCache#LruCache(int))`(int maxSize) ` |

|                                                                                                                                                    ### Public methods                                                                                                                                                    ||
|--------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` final int`       | ` `[createCount](https://developer.android.com/reference/android/support/v4/util/LruCache#createCount())`() ` Returns the number of times[create(Object)](https://developer.android.com/reference/android/support/v4/util/LruCache#create(K))returned a value.                                       |
| ` final void`      | ` `[evictAll](https://developer.android.com/reference/android/support/v4/util/LruCache#evictAll())`() ` Clear the cache, calling[entryRemoved(boolean, K, V, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#entryRemoved(boolean, K, V, V))on each removed entry.      |
| ` final int`       | ` `[evictionCount](https://developer.android.com/reference/android/support/v4/util/LruCache#evictionCount())`() ` Returns the number of values that have been evicted.                                                                                                                               |
| ` final V`         | ` `[get](https://developer.android.com/reference/android/support/v4/util/LruCache#get(K))`(K key) ` Returns the value for`key`if it exists in the cache or can be created by`#create`.                                                                                                               |
| ` final int`       | ` `[hitCount](https://developer.android.com/reference/android/support/v4/util/LruCache#hitCount())`() ` Returns the number of times[get(K)](https://developer.android.com/reference/android/support/v4/util/LruCache#get(K))returned a value that was already present in the cache.                  |
| ` final int`       | ` `[maxSize](https://developer.android.com/reference/android/support/v4/util/LruCache#maxSize())`() ` For caches that do not override[sizeOf(K, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#sizeOf(K, V)), this returns the maximum number of entries in the cache. |
| ` final int`       | ` `[missCount](https://developer.android.com/reference/android/support/v4/util/LruCache#missCount())`() ` Returns the number of times[get(K)](https://developer.android.com/reference/android/support/v4/util/LruCache#get(K))returned null or required a new value to be created.                   |
| ` final V`         | ` `[put](https://developer.android.com/reference/android/support/v4/util/LruCache#put(K, V))`(K key, V value) ` Caches`value`for`key`.                                                                                                                                                               |
| ` final int`       | ` `[putCount](https://developer.android.com/reference/android/support/v4/util/LruCache#putCount())`() ` Returns the number of times[put(K, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#put(K, V))was called.                                                        |
| ` final V`         | ` `[remove](https://developer.android.com/reference/android/support/v4/util/LruCache#remove(K))`(K key) ` Removes the entry for`key`if it exists.                                                                                                                                                    |
| ` void`            | ` `[resize](https://developer.android.com/reference/android/support/v4/util/LruCache#resize(int))`(int maxSize) ` Sets the size of the cache.                                                                                                                                                        |
| ` final int`       | ` `[size](https://developer.android.com/reference/android/support/v4/util/LruCache#size())`() ` For caches that do not override[sizeOf(K, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#sizeOf(K, V)), this returns the number of entries in the cache.               |
| ` final Map<K, V>` | ` `[snapshot](https://developer.android.com/reference/android/support/v4/util/LruCache#snapshot())`() ` Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.                                                                         |
| ` final String`    | ` `[toString](https://developer.android.com/reference/android/support/v4/util/LruCache#toString())`() `                                                                                                                                                                                              |
| ` void`            | ` `[trimToSize](https://developer.android.com/reference/android/support/v4/util/LruCache#trimToSize(int))`(int maxSize) ` Remove the eldest entries until the total of remaining entries is at or below the requested size.                                                                          |

|                                                                                                            ### Protected methods                                                                                                             ||
|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` V`    | ` `[create](https://developer.android.com/reference/android/support/v4/util/LruCache#create(K))`(K key) ` Called after a cache miss to compute a value for the corresponding key.                                                   |
| ` void` | ` `[entryRemoved](https://developer.android.com/reference/android/support/v4/util/LruCache#entryRemoved(boolean, K, V, V))`(boolean evicted, K key, V oldValue, V newValue) ` Called for entries that have been evicted or removed. |
| ` int`  | ` `[sizeOf](https://developer.android.com/reference/android/support/v4/util/LruCache#sizeOf(K, V))`(K key, V value) ` Returns the size of the entry for`key`and`value`in user-defined units.                                        |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v4/util/LruCache#)From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||

## Public constructors

### LruCache

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
LruCache (int maxSize)
```

<br />

<br />

|                                                                                                                                                 Parameters                                                                                                                                                  ||
|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `maxSize` | `int`: for caches that do not override[sizeOf(K, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#sizeOf(K, V)), this is the maximum number of entries in the cache. For all other caches, this is the maximum sum of the sizes of the entries in this cache. <br /> |

## Public methods

### createCount

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int createCount ()
```

Returns the number of times[create(Object)](https://developer.android.com/reference/android/support/v4/util/LruCache#create(K))returned a value.

<br />

|    Returns    ||
|-------|--------|
| `int` | <br /> |

### evictAll

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void evictAll ()
```

Clear the cache, calling[entryRemoved(boolean, K, V, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#entryRemoved(boolean, K, V, V))on each removed entry.

<br />

### evictionCount

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int evictionCount ()
```

Returns the number of values that have been evicted.

<br />

|    Returns    ||
|-------|--------|
| `int` | <br /> |

### get

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
V get (K key)
```

Returns the value for`key`if it exists in the cache or can be created by`#create`. If a value was returned, it is moved to the head of the queue. This returns null if a value is not cached and cannot be created.

<br />

|    Parameters     ||
|-------|------------|
| `key` | `K` <br /> |

|   Returns   ||
|-----|--------|
| `V` | <br /> |

### hitCount

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int hitCount ()
```

Returns the number of times[get(K)](https://developer.android.com/reference/android/support/v4/util/LruCache#get(K))returned a value that was already present in the cache.

<br />

|    Returns    ||
|-------|--------|
| `int` | <br /> |

### maxSize

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int maxSize ()
```

For caches that do not override[sizeOf(K, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#sizeOf(K, V)), this returns the maximum number of entries in the cache. For all other caches, this returns the maximum sum of the sizes of the entries in this cache.

<br />

|    Returns    ||
|-------|--------|
| `int` | <br /> |

### missCount

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int missCount ()
```

Returns the number of times[get(K)](https://developer.android.com/reference/android/support/v4/util/LruCache#get(K))returned null or required a new value to be created.

<br />

|    Returns    ||
|-------|--------|
| `int` | <br /> |

### put

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
V put (K key, 
                V value)
```

Caches`value`for`key`. The value is moved to the head of the queue.

<br />

|     Parameters      ||
|---------|------------|
| `key`   | `K` <br /> |
| `value` | `V` <br /> |

|                    Returns                     ||
|-----|-------------------------------------------|
| `V` | the previous value mapped by`key`. <br /> |

### putCount

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int putCount ()
```

Returns the number of times[put(K, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#put(K, V))was called.

<br />

|    Returns    ||
|-------|--------|
| `int` | <br /> |

### remove

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
V remove (K key)
```

Removes the entry for`key`if it exists.

<br />

|    Parameters     ||
|-------|------------|
| `key` | `K` <br /> |

|                    Returns                     ||
|-----|-------------------------------------------|
| `V` | the previous value mapped by`key`. <br /> |

### resize

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void resize (int maxSize)
```

Sets the size of the cache.

<br />

|                   Parameters                   ||
|-----------|-------------------------------------|
| `maxSize` | `int`: The new maximum size. <br /> |

### size

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int size ()
```

For caches that do not override[sizeOf(K, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#sizeOf(K, V)), this returns the number of entries in the cache. For all other caches, this returns the sum of the sizes of the entries in this cache.

<br />

|    Returns    ||
|-------|--------|
| `int` | <br /> |

### snapshot

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
Map<K, V> snapshot ()
```

Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.

<br />

|       Returns       ||
|-------------|--------|
| `Map<K, V>` | <br /> |

### toString

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
String toString ()
```

<br />

<br />

|     Returns      ||
|----------|--------|
| `String` | <br /> |

### trimToSize

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void trimToSize (int maxSize)
```

Remove the eldest entries until the total of remaining entries is at or below the requested size.

<br />

|                                                     Parameters                                                     ||
|-----------|---------------------------------------------------------------------------------------------------------|
| `maxSize` | `int`: the maximum size of the cache before returning. May be -1 to evict even 0-sized elements. <br /> |

## Protected methods

### create

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
V create (K key)
```

Called after a cache miss to compute a value for the corresponding key. Returns the computed value or null if no value can be computed. The default implementation returns null.

The method is called without synchronization: other threads may access the cache while this method is executing.

If a value for`key`exists in the cache when this method returns, the created value will be released with[entryRemoved(boolean, K, V, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#entryRemoved(boolean, K, V, V))and discarded. This can occur when multiple threads request the same key at the same time (causing multiple values to be created), or when one thread calls[put(K, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#put(K, V))while another is creating a value for the same key.

<br />

|    Parameters     ||
|-------|------------|
| `key` | `K` <br /> |

|   Returns   ||
|-----|--------|
| `V` | <br /> |

### entryRemoved

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void entryRemoved (boolean evicted, 
                K key, 
                V oldValue, 
                V newValue)
```

Called for entries that have been evicted or removed. This method is invoked when a value is evicted to make space, removed by a call to[remove(K)](https://developer.android.com/reference/android/support/v4/util/LruCache#remove(K)), or replaced by a call to[put(K, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#put(K, V)). The default implementation does nothing.

The method is called without synchronization: other threads may access the cache while this method is executing.

<br />

|                                                                                                                                                                     Parameters                                                                                                                                                                      ||
|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `evicted`  | `boolean`: true if the entry is being removed to make space, false if the removal was caused by a[put(K, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#put(K, V))or[remove(K)](https://developer.android.com/reference/android/support/v4/util/LruCache#remove(K)). <br />                               |
| `key`      | `K` <br />                                                                                                                                                                                                                                                                                                                              |
| `oldValue` | `V` <br />                                                                                                                                                                                                                                                                                                                              |
| `newValue` | `V`: the new value for`key`, if it exists. If non-null, this removal was caused by a[put(K, V)](https://developer.android.com/reference/android/support/v4/util/LruCache#put(K, V)). Otherwise it was caused by an eviction or a[remove(K)](https://developer.android.com/reference/android/support/v4/util/LruCache#remove(K)). <br /> |

### sizeOf

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
int sizeOf (K key, 
                V value)
```

Returns the size of the entry for`key`and`value`in user-defined units. The default implementation returns 1 so that size is the number of entries and max size is the maximum number of entries.

An entry's size must not change while it is in the cache.

<br />

|     Parameters      ||
|---------|------------|
| `key`   | `K` <br /> |
| `value` | `V` <br /> |

|    Returns    ||
|-------|--------|
| `int` | <br /> |

-

  ## Interfaces

  - [Pools.Pool](https://developer.android.com/reference/android/support/v4/util/Pools.Pool)
-

  ## Classes

  - [ArrayMap](https://developer.android.com/reference/android/support/v4/util/ArrayMap)
  - [ArraySet](https://developer.android.com/reference/android/support/v4/util/ArraySet)
  - [AtomicFile](https://developer.android.com/reference/android/support/v4/util/AtomicFile)
  - [CircularArray](https://developer.android.com/reference/android/support/v4/util/CircularArray)
  - [CircularIntArray](https://developer.android.com/reference/android/support/v4/util/CircularIntArray)
  - [LongSparseArray](https://developer.android.com/reference/android/support/v4/util/LongSparseArray)
  - [LruCache](https://developer.android.com/reference/android/support/v4/util/LruCache)
  - [ObjectsCompat](https://developer.android.com/reference/android/support/v4/util/ObjectsCompat)
  - [Pair](https://developer.android.com/reference/android/support/v4/util/Pair)
  - [PatternsCompat](https://developer.android.com/reference/android/support/v4/util/PatternsCompat)
  - [Pools](https://developer.android.com/reference/android/support/v4/util/Pools)
  - [Pools.SimplePool](https://developer.android.com/reference/android/support/v4/util/Pools.SimplePool)
  - [Pools.SynchronizedPool](https://developer.android.com/reference/android/support/v4/util/Pools.SynchronizedPool)
  - [SimpleArrayMap](https://developer.android.com/reference/android/support/v4/util/SimpleArrayMap)
  - [SparseArrayCompat](https://developer.android.com/reference/android/support/v4/util/SparseArrayCompat)