Java - 检索列表中类型的数量
我有一个清单。该列表可以包含相同枚举类型的多个项目。
假设我有一个枚举:TOY
,其值:BALL
、DOLL
、PLAYSTATION
。我想知道类型为 TOY
的列表中有多少个 PLAYSTATION
项目。 (即 List
玩具)
对此最好的解决方案是什么?我不想每次都重复列表。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用 Apache commons-collections'
HashBag
。它有一个适合您的getCount(Object)
方法。You can use Apache commons-collections'
HashBag
. It has agetCount(Object)
method which will suit you.java.util.Collections
有一个名为Frequency(Collection c, Object type)
的方法。我的问题中的用法:
java.util.Collections
has a method calledfrequency(Collection c, Object type)
.Usage in my question:
为什么不为您正在使用的列表类型创建一个装饰器,该装饰器存储内部添加/删除的每个枚举类型的计数列表。这样您就可以将其用作普通列表,但还可以添加一些额外的功能来查询当前包含的类型的数量。
您需要做的就是重写 add/remove/addAll 等方法并增加计数器,然后再将其传递给真正的列表类型。最好的部分是您可以使用新包装来装饰任何列表类型。
Why don't you create a decorator for the type of list you're using which stores a list of counts for each enum type have been added/removed internally. That way you could use it as a normal list but also add some extra functionality for querying how many of which type are currently contained.
All you'd need to do would be to override the add/remove/addAll etc methods and increment your counters before passing it on to the real list type. The best part about it would be that you could decorate any list type with your new wrapper.
至少,像这样的实用方法
可以让您从代码中的其他地方简洁地引用 PLAYSTATION 的数量。或者,如果您知道列表不太可能更改,则构建
Map
可以让您一次性计算所有项目的计数。At the very least, a utility method like:
Would let you concisely refer to the number of PLAYSTATIONs from elsewhere in the code. Alternatively if you knew the list was unlikely to change, building a
Map<Toy, Integer>
would let you build up the counts for all items once.如果您不想每次都迭代整个集合,另一种选择是编写 ForwardingList 实现。与 HashBag 建议相比,这种方法的主要优点是:
但是,这种方法有一个缺点,因为您必须编写一些管道代码以使其启动并运行。
下面是一个简单的示例,说明了如何做到这一点。请注意,如果您这样做,您应该覆盖所有从列表中添加/删除的方法,否则您可能会处于不一致的状态:
If you don't want to have to iterate over the entire collection each time, another alternative would be to write a ForwardingList implementation. The main benefits of this over the HashBag suggestion are:
There is a downside to this approach however, in that you have to write a bit of plumbing code to get it up and running.
Below is a quick example of how you could do it. Note that if you do this you should override all methods that add/delete from the list, otherwise you may end up in an inconsistent state:
扩展 java.util.List 方法并重写所有 mutator 方法,即用于添加或删除元素的方法以及用于清除列表的方法。添加对私有 java.util.Map 的引用,它将保存每种类型的项目数。添加访问器方法,该方法将返回每种类型的当前元素数。
Extend java.util.List method and override all mutator methods, i.e. the ones that are used for add or delete elements and also ones used to clear the list. Add a reference to a private java.util.Map which will hold the number of items per type. Add accessor methods which will return current number of elements per type.
HashBag(Bozho 开发)似乎是您最好的选择。但更一般的是 Googles Collections 2 具有适当的谓词:
The HashBag (by Bozho) seems to be your best bet. But a bit more general would be Googles Collections 2 with an appropriate Predicate:
除了所有这些解决方案(我对 Collections.Frequency 调用有弱点)之外,我建议您查看 Google 收藏集,特别是 [Collections2.transform][2],它可以让您实时查看项目。
[2]: http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Collections2.html#transform(java.util.Collection, com.google.公共.基础.函数)
Besides all those solutions (I have a weakness for the Collections.Frequency call), i would recommend you to take a look at google collections, and particularly to [Collections2.transform][2], which could give you a live view on items.
[2]: http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Collections2.html#transform(java.util.Collection, com.google.common.base.Function)