简体   繁体   English

Guava MultiSet对比地图?

[英]Guava MultiSet vs Map?

我对Multiset的理解是一个有频率的集合,但是我总是可以使用Map来表示频率,还有其他理由使用Multiset吗?

Advantages of a Multiset<E> over a Map<E, Integer> : Multiset<E>优于Map<E, Integer>优点:

  • No special code required when adding an element that is not already in the collection. 添加尚未包含在集合中的元素时,无需特殊代码。
  • Methods for handling the count of elements directly: count(E) , add(E, int) , etc. 直接处理元素计数的方法: count(E)add(E, int)等。
  • The intention of the code is clearer. 代码的意图更清晰。 A Multiset<E> obviously maps the elements to their counts. Multiset<E>显然将元素映射到它们的计数。 A Map<E, Integer> could map the elements to arbitrary integers. Map<E, Integer>可以将元素映射到任意整数。

See also: 也可以看看:

Multiset Javadoc Multiset Javadoc

Multiset explained in the Guava Wiki Multiset在Guava Wiki中进行了解释

To me, the most important point that sets Multiset apart from a Map is that it's a Collection : you just put stuff into it and you can get counts later. 对我而言,将MultisetMap区别开来的最重要的一点是它是一个Collection :你只需要把东西放进去就可以得到计数。 It conceptually fits the use cases for which it's designed where a Map does not. 它在概念上适合于Map设计的用例。 For those use cases, a Map is just a hack that kinda-sorta works OK since Java didn't provide anything more appropriate. 对于那些用例, Map只是一种有点排序的hack,因为Java没有提供更合适的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM