在Eclipse Collections中具有软值的映射

huangapple go评论93阅读模式
英文:

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&lt;Integer, Long[]&gt; cache = new ReferenceMap&lt;&gt;();

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目前没有WeakValueHashMapSoftValueHashMap。我也想不出一种在没有实现它们的情况下能够以最小样板代码建立它们的方法。如果您想使用Eclipse Collections的MutableMap API而不是Map,您可以适应上面的ReferenceMap

private final MutableMap<Integer, Long[]> cache = Maps.adapt(new ReferenceMap<>());

这不会帮助您摆脱Apache Commons的依赖,但会让您更接近一致的API使用。

如果您有兴趣为Eclipse Collections做贡献,肯定有实现WeakValueHashMapSoftValueHashMap的空间。

英文:

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&lt;Integer, Long[]&gt; cache = Maps.adapt(new ReferenceMap&lt;&gt;());

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.

huangapple
  • 本文由 发表于 2020年8月28日 08:03:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63625672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定