Skip to content

weakKeys #523

Answered by ben-manes
Jiej009 asked this question in Q&A
Mar 26, 2021 · 1 comment
Discussion options

You must be logged in to vote

This is a carry over from Guava, where @kevinb9n explained the rational. There can be confusions because when the key that created the entry is GC'd then the entry is discarded. If you expect to find the entry by another equivalent key then your application may not hold the key and will observe more cache misses. In these cases, it may make more sense to use weakValues.

The primary case where weakKeys with equality may make sense is for a weak interner. This is what Guava does in Interners.weak(). The explicit contract of this interface is to discard duplicates and reuse a single instance from the cache (Flyweight pattern). In this case Guava uses a package-private keyEquivalence feature.…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ben-manes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants