A {@link ConcurrentMap} builder, providing any combination of thesefeatures: {@linkplain SoftReference soft} or {@linkplain WeakReference weak} keys, soft or weak values, timed expiration, and on-demandcomputation of values. Usage example:
{@code ConcurrentMap graphs = new MapMaker() .concurrencyLevel(32) .softKeys() .weakValues() .expiration(30, TimeUnit.MINUTES) .makeComputingMap(}new Function() public Graph apply(Key key) { return createExpensiveGraph(key); } });}
These features are all optional; {@code new MapMaker().makeMap()}returns a valid concurrent map that behaves exactly like a {@link ConcurrentHashMap}. The returned map is implemented as a hash table with similar performance characteristics to {@link ConcurrentHashMap}. It supports all optional operations of the {@code ConcurrentMap} interface. It does not permitnull keys or values. It is serializable; however, serializing a map that uses soft or weak references can give unpredictable results.
Note: by default, the returned map uses equality comparisons (the {@link Object#equals(Object) equals} method) to determine equalityfor keys or values. However, if {@link #weakKeys()} or {@link #softKeys()} was specified, the map uses identity ({@code ==}) comparisons instead for keys. Likewise, if {@link #weakValues()} or{@link #softValues()} was specified, the map uses identity comparisonsfor values.
The returned map has weakly consistent iteration: an iterator over one of the map's view collections may reflect some, all or none of the changes made to the map after the iterator was created.
An entry whose key or value is reclaimed by the garbage collector immediately disappears from the map. (If the default settings of strong keys and strong values are used, this will never happen.) The client can never observe a partially-reclaimed entry. Any {@link java.util.Map.Entry}instance retrieved from the map's {@linkplain Map#entrySet() entry set}is snapshot of that entry's state at the time of retrieval.
{@code new MapMaker().weakKeys().makeMap()} can almost always beused as a drop-in replacement for {@link java.util.WeakHashMap}, adding concurrency, asynchronous cleanup, identity-based equality for keys, and great flexibility.
@author Bob Lee
@author Kevin Bourrillion