Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/util/LruCache#pubctors) \| [Methods](https://developer.android.com/reference/android/util/LruCache#pubmethods) \| [Protected Methods](https://developer.android.com/reference/android/util/LruCache#promethods) \| [Inherited Methods](https://developer.android.com/reference/android/util/LruCache#inhmethods)

# LruCache

*** ** * ** ***

` public class LruCache `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.util.LruCache\<K, V\> |

<br />

*** ** * ** ***

A cache that holds strong references to a limited number of values. Each time a value is accessed, it is moved to the head of a queue. When a value is added to a full cache, the value at the end of that queue is evicted and may become eligible for garbage collection.

If your cached values hold resources that need to be explicitly released, override `https://developer.android.com/reference/android/util/LruCache#entryRemoved(boolean,%20K,%20V,%20V)`.

If a cache miss should be computed on demand for the corresponding keys, override `https://developer.android.com/reference/android/util/LruCache#create(K)`. This simplifies the calling code, allowing it to assume a value will always be returned, even when there's a cache miss.

By default, the cache size is measured in the number of entries. Override `https://developer.android.com/reference/android/util/LruCache#sizeOf(K,%20V)` to size the cache in different units. For example, this cache is limited to 4MiB of bitmaps:

    int cacheSize = 4 * 1024 * 1024; // 4MiB
      LruCache<String, Bitmap> bitmapCache = new LruCache<String, Bitmap>(cacheSize) {
          protected int sizeOf(String key, Bitmap value) {
              return value.getByteCount();
          }
      }

This class is thread-safe. Perform multiple cache operations atomically by synchronizing on the cache:

    synchronized (cache) {
        if (cache.get(key) == null) {
            cache.put(key, value);
        }
      }

This class does not allow null to be used as a key or value. A return value of null from `https://developer.android.com/reference/android/util/LruCache#get(K)`, `https://developer.android.com/reference/android/util/LruCache#put(K,%20V)` or `https://developer.android.com/reference/android/util/LruCache#remove(K)` is unambiguous: the key was not in the cache.

This class appeared in Android 3.1 (Honeycomb MR1); it's available as part of [Android's Support Package](http://developer.android.com/sdk/compatibility-library.html) for earlier releases.

## Summary

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

| ### Public methods ||
|---|---|
| ` final int` | ` https://developer.android.com/reference/android/util/LruCache#createCount()() ` Returns the number of times `https://developer.android.com/reference/android/util/LruCache#create(K)` returned a value. |
| ` final void` | ` https://developer.android.com/reference/android/util/LruCache#evictAll()() ` Clear the cache, calling `https://developer.android.com/reference/android/util/LruCache#entryRemoved(boolean,%20K,%20V,%20V)` on each removed entry. |
| ` final int` | ` https://developer.android.com/reference/android/util/LruCache#evictionCount()() ` Returns the number of values that have been evicted. |
| ` final V` | ` https://developer.android.com/reference/android/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` | ` https://developer.android.com/reference/android/util/LruCache#hitCount()() ` Returns the number of times `https://developer.android.com/reference/android/util/LruCache#get(K)` returned a value that was already present in the cache. |
| ` final int` | ` https://developer.android.com/reference/android/util/LruCache#maxSize()() ` For caches that do not override `https://developer.android.com/reference/android/util/LruCache#sizeOf(K,%20V)`, this returns the maximum number of entries in the cache. |
| ` final int` | ` https://developer.android.com/reference/android/util/LruCache#missCount()() ` Returns the number of times `https://developer.android.com/reference/android/util/LruCache#get(K)` returned null or required a new value to be created. |
| ` final V` | ` https://developer.android.com/reference/android/util/LruCache#put(K,%20V)(K key, V value) ` Caches `value` for `key`. |
| ` final int` | ` https://developer.android.com/reference/android/util/LruCache#putCount()() ` Returns the number of times `https://developer.android.com/reference/android/util/LruCache#put(K,%20V)` was called. |
| ` final V` | ` https://developer.android.com/reference/android/util/LruCache#remove(K)(K key) ` Removes the entry for `key` if it exists. |
| ` void` | ` https://developer.android.com/reference/android/util/LruCache#resize(int)(int maxSize) ` Sets the size of the cache. |
| ` final int` | ` https://developer.android.com/reference/android/util/LruCache#size()() ` For caches that do not override `https://developer.android.com/reference/android/util/LruCache#sizeOf(K,%20V)`, this returns the number of entries in the cache. |
| ` final https://developer.android.com/reference/java/util/Map<K, V>` | ` https://developer.android.com/reference/android/util/LruCache#snapshot()() ` Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed. |
| ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/util/LruCache#toString()() ` Returns a string representation of the object. |
| ` void` | ` https://developer.android.com/reference/android/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` | ` https://developer.android.com/reference/android/util/LruCache#create(K)(K key) ` Called after a cache miss to compute a value for the corresponding key. |
| ` void` | ` https://developer.android.com/reference/android/util/LruCache#entryRemoved(boolean,%20K,%20V,%20V)(boolean evicted, K key, V oldValue, V newValue) ` Called for entries that have been evicted or removed. |
| ` int` | ` https://developer.android.com/reference/android/util/LruCache#sizeOf(K,%20V)(K key, V value) ` Returns the size of the entry for `key` and `value` in user-defined units. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public constructors

### LruCache

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public LruCache (int maxSize)
```

<br />

<br />

| Parameters ||
|---|---|
| `maxSize` | `int`: for caches that do not override `https://developer.android.com/reference/android/util/LruCache#sizeOf(K,%20V)`, 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 [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final int createCount ()
```

Returns the number of times `https://developer.android.com/reference/android/util/LruCache#create(K)` returned a value.

<br />

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

### evictAll

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final void evictAll ()
```

Clear the cache, calling `https://developer.android.com/reference/android/util/LruCache#entryRemoved(boolean,%20K,%20V,%20V)` on each removed entry.

<br />

### evictionCount

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final int evictionCount ()
```

Returns the number of values that have been evicted.

<br />

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

### get

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final 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 [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final int hitCount ()
```

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

<br />

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

### maxSize

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final int maxSize ()
```

For caches that do not override `https://developer.android.com/reference/android/util/LruCache#sizeOf(K,%20V)`, 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 [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final int missCount ()
```

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

<br />

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

### put

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final 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 [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final int putCount ()
```

Returns the number of times `https://developer.android.com/reference/android/util/LruCache#put(K,%20V)` was called.

<br />

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

### remove

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final 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 [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

Sets the size of the cache.

<br />

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

### size

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final int size ()
```

For caches that do not override `https://developer.android.com/reference/android/util/LruCache#sizeOf(K,%20V)`, 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 [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final 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 ||
|---|---|
| `https://developer.android.com/reference/java/util/Map<K, V>` | <br /> |

### toString

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final String toString ()
```

Returns a string representation of the object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representation of the object. <br /> |

### trimToSize

Added in [API level 17](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public 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 [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected 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 `https://developer.android.com/reference/android/util/LruCache#entryRemoved(boolean,%20K,%20V,%20V)` 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 `https://developer.android.com/reference/android/util/LruCache#put(K,%20V)` while another is creating a value for the same key.

<br />

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

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

### entryRemoved

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected 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 `https://developer.android.com/reference/android/util/LruCache#remove(K)`, or replaced by a call to `https://developer.android.com/reference/android/util/LruCache#put(K,%20V)`. 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 `https://developer.android.com/reference/android/util/LruCache#put(K,%20V)` or `https://developer.android.com/reference/android/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 `https://developer.android.com/reference/android/util/LruCache#put(K,%20V)` or a `https://developer.android.com/reference/android/util/LruCache#get(K)`. Otherwise it was caused by an eviction or a `https://developer.android.com/reference/android/util/LruCache#remove(K)`. <br /> |

### sizeOf

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected 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 /> |