Skip to content

Most visited

Recently visited

navigation
added in version 22.1.0
belongs to Maven artifact com.android.support:support-compat:27.0.0

LruCache

public class LruCache
extends Object

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


Static library version of 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(int maxSize)

Public methods

final int createCount()

Returns the number of times create(Object) returned a value.

final void evictAll()

Clear the cache, calling entryRemoved(boolean, K, V, V) on each removed entry.

final int evictionCount()

Returns the number of values that have been evicted.

final V get(K key)

Returns the value for key if it exists in the cache or can be created by #create.

final int hitCount()

Returns the number of times get(K) returned a value that was already present in the cache.

final int maxSize()

For caches that do not override sizeOf(K, V), this returns the maximum number of entries in the cache.

final int missCount()

Returns the number of times get(K) returned null or required a new value to be created.

final V put(K key, V value)

Caches value for key.

final int putCount()

Returns the number of times put(K, V) was called.

final V remove(K key)

Removes the entry for key if it exists.

void resize(int maxSize)

Sets the size of the cache.

final int size()

For caches that do not override sizeOf(K, V), this returns the number of entries in the cache.

final Map<K, V> snapshot()

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

final String toString()
void trimToSize(int maxSize)

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

Protected methods

V create(K key)

Called after a cache miss to compute a value for the corresponding key.

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

Called for entries that have been evicted or removed.

int sizeOf(K key, V value)

Returns the size of the entry for key and value in user-defined units.

Inherited methods

From class java.lang.Object

Public constructors

LruCache

added in version 22.1.0
LruCache (int maxSize)

Parameters
maxSize int: for caches that do not override 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.

Public methods

createCount

added in version 22.1.0
int createCount ()

Returns the number of times create(Object) returned a value.

Returns
int

evictAll

added in version 22.1.0
void evictAll ()

Clear the cache, calling entryRemoved(boolean, K, V, V) on each removed entry.

evictionCount

added in version 22.1.0
int evictionCount ()

Returns the number of values that have been evicted.

Returns
int

get

added in version 22.1.0
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.

Parameters
key K

Returns
V

hitCount

added in version 22.1.0
int hitCount ()

Returns the number of times get(K) returned a value that was already present in the cache.

Returns
int

maxSize

added in version 22.1.0
int maxSize ()

For caches that do not override 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.

Returns
int

missCount

added in version 22.1.0
int missCount ()

Returns the number of times get(K) returned null or required a new value to be created.

Returns
int

put

added in version 22.1.0
V put (K key, 
                V value)

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

Parameters
key K

value V

Returns
V the previous value mapped by key.

putCount

added in version 22.1.0
int putCount ()

Returns the number of times put(K, V) was called.

Returns
int

remove

added in version 22.1.0
V remove (K key)

Removes the entry for key if it exists.

Parameters
key K

Returns
V the previous value mapped by key.

resize

added in version 22.1.0
void resize (int maxSize)

Sets the size of the cache.

Parameters
maxSize int: The new maximum size.

size

added in version 22.1.0
int size ()

For caches that do not override 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.

Returns
int

snapshot

added in version 22.1.0
Map<K, V> snapshot ()

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

Returns
Map<K, V>

toString

added in version 22.1.0
String toString ()

Returns
String

trimToSize

added in version 22.1.0
void trimToSize (int maxSize)

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

Parameters
maxSize int: the maximum size of the cache before returning. May be -1 to evict even 0-sized elements.

Protected methods

create

added in version 22.1.0
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) 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) while another is creating a value for the same key.

Parameters
key K

Returns
V

entryRemoved

added in version 22.1.0
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), or replaced by a call to 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.

Parameters
evicted boolean: true if the entry is being removed to make space, false if the removal was caused by a put(K, V) or remove(K).

key K

oldValue V

newValue V: the new value for key, if it exists. If non-null, this removal was caused by a put(K, V). Otherwise it was caused by an eviction or a remove(K).

sizeOf

added in version 22.1.0
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.

Parameters
key K

value V

Returns
int

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)