Java 集合:如何将排序列表划分为子列表
假设我有一个列表(EG:LinkedList
),其中包含按某个属性排序的元素(EG:SomeObject.someValue()
)。此属性通常可以并且通常确实经常重复/它不是唯一的,但有没有
一种方便的方法可以将其划分为多个列表,每个列表仅包含其基本顺序相同的列表?另外,可以仅通过列表的一次迭代来完成此操作吗? ? 例如,原始列表:
1, 1, 1, 2, 2, 3, 3, 3
所需的列表来自:
1, 1, 1
2, 2,
3, 3, 3
Let's say I have a list (EG: LinkedList<SomeObject>
that contains elements ordered by a certain attribute (EG: SomeObject.someValue()
). This attribute can and usually does repeat often/it isn't unique, BUT is never null.
Is there a convenient way to divide this into multiple Lists, each list containing only its equal in cardinal order? Also, can this be done with only once iteration of the list? For example, the original list:
1, 1, 1, 2, 2, 3, 3, 3
The desired lists from this:
1, 1, 1
2, 2,
3, 3, 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不太方便,但是:
equals(..)
,并小心null
),则创建一个新的List
,或使用list.subList(groupStart, currentIdx)Not too convenient, but:
equals(..)
, and be careful withnull
), then create a newList
, or uselist.subList(groupStart, currentIdx)
您可以使用 Apache CollectionUtils 来执行此操作,其中“list”是原始列表,“value”是要为其提取子列表的对象的当前值:
这种方法意味着使用众所周知且经过良好测试的库(这总是一件好事),但是缺点是对于您需要的每个子列表,您将循环遍历列表一次。
You could use Apache CollectionUtils to do this, where "list" is the original list, and "value" is the current value of the objects you want to extract a sublist for:
This approach means using a well known and well tested library (which always is a good thing), but the downside is that you will loop through the list once for each sublist you need.
很确定没有 Java API 方法可以实现这一点。但是您可以这样写:
这将返回一个子列表的
HashMap
,其中键是someValue
,值是与其关联的分区列表。注意,我没有对此进行测试,所以不要只是复制代码。编辑:使此返回哈希图而不是数组列表。
Pretty sure there isn't a java API method for this. However you can write:
This will return you an
HashMap
of sublists, where the key is thesomeValue
and the value is the partitioned list associated to it. Note, I didn't test this, so don't just copy the code.EDIT: made this return hashmap instead of arraylist.
如果您使用 Google Guava-libaries:
输出:
如果您需要计算有多少个x 的元素使用
ints.count(x);
,如果您有值类型,则不需要更多,只需计数即可。If you would use Google Guava-libaries:
Output:
If you need to count how many elements of x you have use
ints.count(x);
, if you have value types you do not need to have more then just count.对于 Guava,请使用
Multimaps.index (Iterable, Function)
。With Guava, use
Multimaps.index(Iterable<V>, Function<? super V, K>)
.这应该有效(未经测试,但我很确定一切正常,这也假设列表的内容是可排序的):
This should work (untested, but I am pretty sure everything is ok, This also assumes that the contents of the list are sortable):