public class LazyLoadingMap<K,T> extends HashMap<K,T>
HashMap that optionally uses Suppliers to provide its
values. Each Supplier is called at most once, if the corresponding
value is requested. Some "global" operations requires all values
to be computed, and should be considered costly.
Like HashMap, this class is NOT thread safe. If needed,
Collections.synchronizedMap(java.util.Map<K, V>) can be used
to synchronize it.
Note that remove(java.lang.Object) behaves slightly differently than
the HashMap version.| Modifier and Type | Class and Description |
|---|---|
class |
LazyLoadingMap.Stats
Statistics on Supplier usage
|
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>| Constructor and Description |
|---|
LazyLoadingMap() |
| Modifier and Type | Method and Description |
|---|---|
void |
clear() |
LazyLoadingMap<K,T> |
computeAllThrowsException(boolean b)
Optionally throw a RuntimeException if computeAll is called
|
LazyLoadingMap<K,T> |
computeValueOnRemove(boolean b)
Optionally compute the value on remove, so that it doesn't return null
|
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value)
Calls computeAll - should be avoided if possible
|
Set<Map.Entry<K,T>> |
entrySet()
Calls computeAll - should be avoided if possible
|
boolean |
equals(Object o)
Calls computeAll - should be avoided if possible
|
T |
get(Object key) |
LazyLoadingMap.Stats |
getStats()
Return statistics on our suppliers, for metrics etc.
|
int |
hashCode() |
boolean |
isEmpty() |
Set<K> |
keySet() |
Supplier<T> |
put(K key,
Supplier<T> supplier)
Adds a Supplier that provides a lazy loaded value.
|
T |
remove(Object key)
Unless
computeValueOnRemove(boolean) is set to
true, this returns null to avoid calling a supplier
"for nothing". |
int |
size() |
Collection<T> |
values()
Calls computeAll - should be avoided if possible
|
clone, compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, put, putAll, putIfAbsent, remove, replace, replace, replaceAlltoStringpublic boolean equals(Object o)
public int hashCode()
public Supplier<T> put(K key, Supplier<T> supplier)
public T remove(Object key)
computeValueOnRemove(boolean) is set to
true, this returns null to avoid calling a supplier
"for nothing".public void clear()
public boolean containsKey(Object key)
containsKey in interface Map<K,T>containsKey in class HashMap<K,T>public int size()
public boolean isEmpty()
public Collection<T> values()
public boolean containsValue(Object value)
containsValue in interface Map<K,T>containsValue in class HashMap<K,T>public LazyLoadingMap.Stats getStats()
public LazyLoadingMap<K,T> computeValueOnRemove(boolean b)
public LazyLoadingMap<K,T> computeAllThrowsException(boolean b)
Copyright © 2007–2021 The Apache Software Foundation. All rights reserved.