わかりました、このようなものはどうですか (ただし、あなたがまだ苦労しているのを見てすぐにこれをまとめました - カウントを維持するためのマップの使用は素晴らしいものではありません):
ログを次のように変更します。
Integer id;
DateTime time;
String value;
public Log(Integer id, Timestamp time, String value) {
this.id = id;
this.time = new DateTime(time.getTime());
this.value = value;
}
それで:
// Build some fake logs
final List<Log> logs = new ArrayList<Log>();
logs.add(new Log(1, new Timestamp(new DateTime().withDate(2011,1,1).getMillis()), "90"));
logs.add(new Log(1, new Timestamp(new DateTime().withDate(2010,1,1).withTimeAtStartOfDay().getMillis()), "60"));
logs.add(new Log(1, new Timestamp(new DateTime().withDate(2010,1,1).withTimeAtStartOfDay().getMillis()), "80"));
logs.add(new Log(1, new Timestamp(new DateTime().withDate(2009,1,1).getMillis()), "70"));
final Map<DateTime, Integer> total = new HashMap<DateTime, Integer>();
final Map<DateTime, Integer> count = new HashMap<DateTime, Integer>();
for(Log l : logs) {
// Round down to the nearest minute
final DateTime t = l.getTime().withSecondOfMinute(0);
Integer sum = total.get(t);
sum = (sum == null) ? 0 : sum;
total.put(t, sum + Integer.parseInt(l.getValue()));
Integer c = count.get(t);
c = (c == null) ? 0 : c;
count.put(t, c + 1);
}
for(Map.Entry<DateTime, Integer> entry : total.entrySet()) {
System.out.println(entry.getValue() / count.get(entry.getKey()));
}
ここでは、引き続きコンストラクターで使用することを想定していますがTimeStamp
、 JodaTimeオブジェクトLog
として内部に格納してもかまいません。DateTime
次に、いくつかの偽のログを作成します。次に、すべてのログをループして、カウントと合計の両方を最も近い分に保存します。現実的には、このデータ (合計とカウントの両方) を格納するオブジェクトを作成し、それをDateTime
キーとして使用します。これが理にかなっていることを願っています。