java为什么是MapMaker。软键()已弃用?
Possible Duplicate:
Why is softKeys() deprecated in Guava 10?
在JavaDoc中MapMaker.softKeys()
被弃用的原因是:
Use softValues to create a memory-sensitive map, or weakKeys to create a map that doesn't hold strong references to the keys. This method is scheduled for deletion in January 2013.
This method is broken. Maps with soft keys offer no functional advantage over maps with weak keys, and they waste memory by keeping unreachable elements in the map. If your goal is to create a memory-sensitive map, then consider using soft values instead.
Specifies that each key (not value) stored in the map should be wrapped in a SoftReference (by default, strong references are used). Softly-referenced objects will be garbage-collected in a globally least-recently-used manner, in response to memory demand.
我不明白为什么软价值比软键更受欢迎?有什么情况吗MapMaker.softValues()
仍然可用。谢谢
# 1 楼答案
带有软项和弱项的地图有两个主要用例:
缓存。具有软值的映射可以充当内存敏感缓存。由于软引用的语义,在需要空闲内存时会收集缓存的条目
规范化映射。有时需要将其他属性(值)与现有对象(键)相关联。此关联不应阻止收集密钥,而且在收集密钥后也没有意义。这种关联可以实现为带有弱密钥的映射
请注意,在后一种情况下,使用软键而不是软键没有任何意义,并且会导致不必要的内存使用,因此软键没有实际用途,这就是为什么不推荐使用软键的原因