-5

我有两个实体:

class Entry {
   long amount;
   long date; //date is stored in millis
   long id;
   //getter and setters
}

class GroupedEntryByWeek {
   long amount;
   long startDate; // start date of week
   long endDate; // end date of week
   long id;
}

现在我想“ List<Entry> entries”生成新列表“ List<GroupedEntryByWeek> groupedEntries

我想创建一个函数:

public List<GroupedEntryByWeek> groupEntries(List<Entry> entries) {
   List<GroupedEntryByWeek> groupedEntries;
   // code for grouping         

   return groupedEntries;
}

这将返回我一个分组列表

Ungrouped list :

id        amount       date
1          500         9th june in millies 
2          600         8th june in millies
3          700         1st june in millies

分组列表应如下所示:

    id        amount       startdate   enddate 
    1          700         28May       3June
    2          1100        4june       9june
4

1 回答 1

2

如果我正确理解了您需要List<GroupedEntryByWeek>Entry课堂上创建实例的问题。

像这样:

class Entry {
   long amount;
   long date; //date is stored in millis
   long id;

   List<GroupedEntryByWeek> groupedEntries;

   //getter and setters
}

然后,当您遍历条目列表时,您会从每个条目中获得分组条目:

for (Entry entry : entries) {
   List<GroupedEntryByWeek> groupedEntries = entry.getGroupedEntries();
}
于 2012-06-09T17:41:49.567 回答