< prev index next >

src/java.base/share/classes/jdk/internal/util/ReferencedKeyMap.java

Print this page

 31 import java.lang.ref.WeakReference;
 32 import java.util.AbstractMap;
 33 import java.util.Collection;
 34 import java.util.HashMap;
 35 import java.util.Objects;
 36 import java.util.Map;
 37 import java.util.Set;
 38 import java.util.concurrent.ConcurrentHashMap;
 39 import java.util.function.Supplier;
 40 import java.util.function.UnaryOperator;
 41 import java.util.stream.Collectors;
 42 import java.util.stream.Stream;
 43 
 44 import jdk.internal.access.SharedSecrets;
 45 
 46 /**
 47  * This class provides management of {@link Map maps} where it is desirable to
 48  * remove entries automatically when the key is garbage collected. This is
 49  * accomplished by using a backing map where the keys are either a
 50  * {@link WeakReference} or a {@link SoftReference}.

 51  * <p>
 52  * To create a {@link ReferencedKeyMap} the user must provide a {@link Supplier}
 53  * of the backing map and whether {@link WeakReference} or
 54  * {@link SoftReference} is to be used.
 55  *
 56  * {@snippet :
 57  * // Use HashMap and WeakReference
 58  * Map<Long, String> map = ReferencedKeyMap.create(false, HashMap::new);
 59  * map.put(10_000_000L, "a");
 60  * map.put(10_000_001L, "b");
 61  * map.put(10_000_002L, "c");
 62  * map.put(10_000_003L, "d");
 63  * map.put(10_000_004L, "e");
 64  *
 65  * // Use ConcurrentHashMap and SoftReference
 66  * map = ReferencedKeyMap.create(true, ConcurrentHashMap::new);
 67  * map.put(20_000_000L, "v");
 68  * map.put(20_000_001L, "w");
 69  * map.put(20_000_002L, "x");
 70  * map.put(20_000_003L, "y");

 31 import java.lang.ref.WeakReference;
 32 import java.util.AbstractMap;
 33 import java.util.Collection;
 34 import java.util.HashMap;
 35 import java.util.Objects;
 36 import java.util.Map;
 37 import java.util.Set;
 38 import java.util.concurrent.ConcurrentHashMap;
 39 import java.util.function.Supplier;
 40 import java.util.function.UnaryOperator;
 41 import java.util.stream.Collectors;
 42 import java.util.stream.Stream;
 43 
 44 import jdk.internal.access.SharedSecrets;
 45 
 46 /**
 47  * This class provides management of {@link Map maps} where it is desirable to
 48  * remove entries automatically when the key is garbage collected. This is
 49  * accomplished by using a backing map where the keys are either a
 50  * {@link WeakReference} or a {@link SoftReference}.
 51  * Keys must be {@linkplain Class#isIdentity() identity objects.}
 52  * <p>
 53  * To create a {@link ReferencedKeyMap} the user must provide a {@link Supplier}
 54  * of the backing map and whether {@link WeakReference} or
 55  * {@link SoftReference} is to be used.
 56  *
 57  * {@snippet :
 58  * // Use HashMap and WeakReference
 59  * Map<Long, String> map = ReferencedKeyMap.create(false, HashMap::new);
 60  * map.put(10_000_000L, "a");
 61  * map.put(10_000_001L, "b");
 62  * map.put(10_000_002L, "c");
 63  * map.put(10_000_003L, "d");
 64  * map.put(10_000_004L, "e");
 65  *
 66  * // Use ConcurrentHashMap and SoftReference
 67  * map = ReferencedKeyMap.create(true, ConcurrentHashMap::new);
 68  * map.put(20_000_000L, "v");
 69  * map.put(20_000_001L, "w");
 70  * map.put(20_000_002L, "x");
 71  * map.put(20_000_003L, "y");
< prev index next >