@dispatch-indirect9206

WeakHashMap fits if you have:
- an operation that's a expensive, but still okay to redo if they're evicted early
- the results take up enough memory that you care about evicting them
- no need for cache invalidation
- you only need single-threaded access

If it fits, WHM is very simple and cheap. But if you're banging your heaad against it, look at Guava CacheBuilder or Apache Commons Pools.

@KevinSheppard

Next question: What is a strong reference?

@severoon

Interesting hack: If you want to control whether an entry can be auto removed, reference the key in the value to hold it in the map until you're okay letting it get garbage collected, at which point you can invite a method on the value to drop the reference to the key.

@pro_gamer29569

How can i get the cup like yours.

@Matt_Pan

But using it with Integer as the key, like in the example, also looks weird as a use case for WeakHashMap
Not only because -128 to 127 is cached anyway (and thus will not be garbage collected) - but again regarding garbage collecting when the keys are no longer in use

@wjblazkowicz

Why does documentation says to use SoftReference for memory sensitive caches, and WeakReference is better for other applications (mapping) because it can be collected earlier.

@omnamahsivay1

Supports in java 8?

@terribleprogrammer

Is it safe in multi threading code?

@peacemakerle

Yeah, this map is very weak. The main use case is caching, but it implementation demands synchronisation. That makes is a bad choice for most use cases.
Could cry every time I see this thing.

@mekud

I don't like the coffee drinking bit. The coffee mug distracts from the main point.