英文:
Map with soft values in Eclipse Collections
问题
我目前正在考虑在我参与的项目中 consol,然后尽管我对此经验有限,但我真的很喜欢 Eclipse Collections 的外观。
我们有一个具体的用例,即我们在多个地方使用带有软值引用的 Map
实现作为简单的缓存。目前,我们在这个目的上使用 Apache Commons Collections 的 ReferenceMap
,例如:
private final Map<Integer, Long[]> cache = new ReferenceMap<>();
当然,这可以被标准的 Java HashMap
替代,其中值是 SoftReference
实现,但我希望 Eclipse Collections 有一个类似的“方便”集合类型或用于此目的的工厂/构建器。是否有这样的东西?或更广泛地说,我如何使用 Eclipse Collections 设置一个带有软值引用的 Map,同时尽量少使用样板代码?
英文:
I am currently looking at consolidating to a single collections library in a project I'm involved in, and though I have little experience with it, I really like the look of Eclipse Collections.
One concrete use case we have is that we use a Map
implementation with soft value references in several places as a simple cache. We currently use Apache Commons Collections' ReferenceMap
for this purpose, for example:
private final Map<Integer, Long[]> cache = new ReferenceMap<>();
Of course this can be replaced by a standard Java HashMap
where the value is an SoftReference
implementation, but I was hoping Eclipse Collections had a similar "convenience" collection type or factory/builder for this purpose. Is there such a thing? Or perhaps more broadly formulated: how would I set up a Map with soft value references using Eclipse Collections and a minimum amount of boilerplate?
答案1
得分: 2
Eclipse Collections目前没有WeakValueHashMap
或SoftValueHashMap
。我也想不出一种在没有实现它们的情况下能够以最小样板代码建立它们的方法。如果您想使用Eclipse Collections的MutableMap
API而不是Map
,您可以适应上面的ReferenceMap
。
private final MutableMap<Integer, Long[]> cache = Maps.adapt(new ReferenceMap<>());
这不会帮助您摆脱Apache Commons的依赖,但会让您更接近一致的API使用。
如果您有兴趣为Eclipse Collections做贡献,肯定有实现WeakValueHashMap
和SoftValueHashMap
的空间。
英文:
Eclipse Collections does not currently have a WeakValueHashMap
or SoftValueHashMap
. I also can't think off hand of a way of setting one up with minimal boiler plate without implementing them. If you want to use the Eclipse Collections MutableMap
API instead of Map
, you can adapt the ReferenceMap
above.
private final MutableMap<Integer, Long[]> cache = Maps.adapt(new ReferenceMap<>());
This won't help you get rid of the Apache Commons dependency, but gets you a step closer to a consistent API usage.
If you are interested in contributing to Eclipse Collections, there is certainly room for WeakValueHashMap
and SoftValueHashMap
implementations.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论