Guava Cache 中的weakKeys() 和weakValues() 有什么不同?
我知道 weakKeys()
意味着键被 WeakReference
包装,GC 可以自动收集键,与 weakValues()
相同(对于值) 。
我的理解:一旦一个条目(无论是键还是值)被GC收集,该条目就过期了。
我的问题:
weakKeys()
和weakValues()
等效吗? (其中之一被收集,该条目已过期)- Guava doc说:“当使用此方法时,生成的缓存将使用identity(==)比较来确定值的相等性”。缓存什么时候比较值?为了什么?
I know weakKeys()
means the keys are wrapped by WeakReference
and GC may collect the keys automatically, same with weakValues()
(for value).
My understanding: Once an entry (whether key or value) was collected by GC, the entry is expired.
My question:
- Are
weakKeys()
andweakValues()
equivalent? (one of them was collected, the entry is expired) - Guava doc said: "when this method is used, the resulting cache will use identity (==) comparison to determine equality of values". When the cache compares the value? for what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,如果收集了某个键或值,则
Entry
本身不会过期。您仍然可以访问Entry
,但对键和值的“引用”会将您指向null
。在 Java 中,对象变量基本上是指针,这意味着它们在包含对象的内存块中存储一个 8 位地址。因此,文档说它不会比较对象(因为它们是
null
),而是比较对象的引用。No, if a key or value is collected, the
Entry
itself will not expire. You will still have access to theEntry
, but the "reference" to the key and value will point you tonull
.In Java,
Object
variables are basically pointers, which means they store an8-bit
address in the memory block containing the object. So, the documentation says that it will not compare objects (because they arenull
), but references to objects.