Java:按类型分组列表元素(Java: Group list elements by type)

编程入门 行业动态 更新时间:2024-10-07 06:38:21
Java:按类型分组列表元素(Java: Group list elements by type)

目前我正在使用Java 6.出于......的原因。 现在我想按照日期对列表中的所有元素进行分组,因此每个日期没有多个条目。

假设我有一个结构上看起来像这样的列表:

+==============+=============+ | dateHappened | pottyUses | +==============+=============+ | 10/09/2015 | 255 | +--------------+-------------+ | 10/09/2015 | 256 | +--------------+-------------+ | 10/09/2015 | 254 | +--------------+-------------+

我想把它变成这个:

+==============+=============+ | dateHappened | pottyUses | +==============+=============+ | 10/09/2015 | 765 | +--------------+-------------+

列表代码如下所示:

public class PottyCollection implements Comparable<PottyCollection> { public PottyCollection(final Date dateHappened, final int pottyUses) { this.dateHappened = dateHappened; this.pottyUses = pottyUses; } final public Date dateHappened; final public int pottyUses; }

到目前为止,我一直在创建两个独立的PottyCollection实例。 第一个(完整)使用Collections.Sort()按Date排序。 第二个(空)然后遍历整个列表,对于我们找到的每个日期,它将在该日期之前递增newPottyUses 。 找到新日期后,它会将所有数据插入新列表,并重置newPottyUses ,然后继续循环直到完成。

对于几件物品,这很好。 对于具有许多不同类型的大型列表,它已达到不易维护的程度。 我不禁觉得这里有一些重新发明的车轮。

有没有更好的办法? Java 6和8解决方案都会受到赞赏,但目前只能检查6个。

Currently I'm using Java 6. For... for reasons. Now I want to group all elements in a list by their date, so I don't have multiple entries for each date.

Let's say I have a list that structurally looks like this:

+==============+=============+ | dateHappened | pottyUses | +==============+=============+ | 10/09/2015 | 255 | +--------------+-------------+ | 10/09/2015 | 256 | +--------------+-------------+ | 10/09/2015 | 254 | +--------------+-------------+

And I want to turn it into this:

+==============+=============+ | dateHappened | pottyUses | +==============+=============+ | 10/09/2015 | 765 | +--------------+-------------+

The list code looks like this:

public class PottyCollection implements Comparable<PottyCollection> { public PottyCollection(final Date dateHappened, final int pottyUses) { this.dateHappened = dateHappened; this.pottyUses = pottyUses; } final public Date dateHappened; final public int pottyUses; }

So far, I've been creating two separate instances of PottyCollection. The first one (full) gets sorted by Date using Collections.Sort(). The second one (empty) then loops through the entire list, and for each date we've found, it will increment newPottyUses by that date. When a new date is found, it will insert all of that data into the new list, and reset newPottyUses, then continue the loop until it's finished.

For a couple items, this is fine. For a large list with a lot of different types, it's getting to the point where it isn't very maintainable. I can't help but feel like there's some wheel reinventing going on here.

Is there a better way? Both Java 6, and 8 solutions would be appreciated, but for now, only 6 can be checked.

最满意答案

使用地图:

Map<Date, PottyCollection> map = new HashMap<>(); for (PottyCollection pc : originalList) { PottyCollection existing = map.get(pc.dateHappened); if (existing == null) { map.put(pc.dateHappened, pc); } else { map.put(pc.dateHappened, new PottyCollection(pc.dateHappened, pc.pottyUses + existing.pottyUses)); } } Collection<PottyCollection> reduced = map.values();

Use a Map:

Map<Date, PottyCollection> map = new HashMap<>(); for (PottyCollection pc : originalList) { PottyCollection existing = map.get(pc.dateHappened); if (existing == null) { map.put(pc.dateHappened, pc); } else { map.put(pc.dateHappened, new PottyCollection(pc.dateHappened, pc.pottyUses + existing.pottyUses)); } } Collection<PottyCollection> reduced = map.values();

更多推荐

本文发布于:2023-07-27 04:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284900.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   类型   列表   Java   type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!