无需迭代即可获取番石榴多重集中元素的实例数
我在番石榴中有一个多重集,我想检索给定元素的实例数,而不迭代这个多重集(我不想迭代,因为我认为迭代需要相当长的时间,因为它会查看所有集合)。
为此,我首先考虑使用多重集的entryset()方法,来获取包含单个实例及其相应计数的集合。然后,将此集合转换为哈希图(其中键是集合的元素,值是它们的实例计数)。因为这样我就可以使用 hashmap 的方法直接从其键中检索值 - 完成!但是,只有当我可以快速地将集合转换为哈希图(无需迭代所有元素)时,这才有意义:可能吗?
(正如我所说,我预计这个问题在多个方面都有缺陷,如果您能阐明我在这里可能犯的概念性错误,我会很高兴。谢谢!)
I have a multiset in guava and I would like to retrieve the number of instances of a given element without iterating over this multiset (I don't want to iterate because I assume that iterating takes quite some time, as it looks through all the collection).
To do that, I was thinking first to use the entryset() method of multiset, to obtain a set with single instances and their corresponding count. Then, transform this set into a hashmap (where keys are the elements of my set, and values are their instance count). Because then I can use the methods of hashmap to directly retrieve a value from its key - done! But this makes sense only if I can transform the set into the hashmap in a quick way (without iterating trhough all elements): is it possible?
(as I said I expect this question to be flawed on multiple counts, I'd be happy if you could shed light on the conceptual mistakes I probably make here. Thx!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需调用
count(element)
在你的多重集上 - 瞧!Simply invoke
count(element)
on your multiset -- voila!你可能知道在 Guava 中
Multiset
是一个接口,而不是一个类。如果您只想知道某个元素的重复次数,请调用
Multiset.count(Object element)
。请忘记我的以下声明:
然后,如果您使用流行的实现
HashMultiset
,则场景下已经有一个HashMap
在工作。也就是说,当
HashMultiset
迭代时,HashMap 也会迭代。无需转变成另一个HashMap。You may know in Guava
Multiset
is an interface, not a class.If you just want to know the repeated number of an element, call
Multiset.count(Object element)
.Please forget my following statement:
Then if you are using a popular implementation
HashMultiset
, there is already aHashMap<E, AtomicInteger>
working under the scene.That is, when the
HashMultiset
iterates, also a HashMap iterates. No need to transform into another HashMap.