目前我正在使用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();更多推荐
发布评论