![JAR search and dependency download from the Maven repository](/logo.png)
co.easimart.WeakValueHashMap Maven / Gradle / Ivy
package co.easimart;
import java.lang.ref.WeakReference;
import java.util.HashMap;
/**
* A HashMap where all the values are weak.
*/
/** package */ class WeakValueHashMap {
private HashMap> map;
public WeakValueHashMap() {
map = new HashMap>();
}
public void put(K key, V value) {
map.put(key, new WeakReference(value));
}
/**
* Returns null if the key isn't in the map, or if it is an expired reference. If it is, then the
* reference is removed from the map.
*/
public V get(K key) {
WeakReference reference = map.get(key);
if (reference == null) {
return null;
}
V value = reference.get();
if (value == null) {
map.remove(key);
}
return value;
}
public void remove(K key) {
map.remove(key);
}
public void clear() {
map.clear();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy