Guava MultiSet 与 Map?
我对Multiset的理解是一个带有频率的集合,但是我总是可以使用Map来表示频率,还有其他原因使用Multiset吗?
My understanding of Multiset is a set with frequency, but I can always use Map to represent the frequency, is there other reason to use Multiset?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Multiset
相对于Map
的优点:count(E)
、add(E, int)
等,Multiset
显然将元素映射到它们的计数。Map
可以将元素映射到任意整数。另请参阅:
Multiset Javadoc
< a href="https://github.com/google/guava/wiki/NewCollectionTypesExplained#multiset" rel="nofollow noreferrer">Guava Wiki 中解释的 Multiset
Advantages of a
Multiset<E>
over aMap<E, Integer>
:count(E)
,add(E, int)
, etc.Multiset<E>
obviously maps the elements to their counts. AMap<E, Integer>
could map the elements to arbitrary integers.See also:
Multiset Javadoc
Multiset explained in the Guava Wiki
对我而言,
Multiset
与Map
的最重要区别在于它是一个Collection
:您只需将内容放入其中即可稍后计数。从概念上讲,它适合其设计的用例,而Map
则不适合。对于这些用例,Map
只是一种可以正常工作的 hack,因为 Java 没有提供任何更合适的东西。To me, the most important point that sets
Multiset
apart from aMap
is that it's aCollection
: you just put stuff into it and you can get counts later. It conceptually fits the use cases for which it's designed where aMap
does not. For those use cases, aMap
is just a hack that kinda-sorta works OK since Java didn't provide anything more appropriate.