我有一个LinkedHashMap<Date, Double>where 键是日期和一个与每个条目关联的 Double 值。每小时有一个条目。
我需要平均一周中的每一天的值,所以我得到一个列表,其中包含第一个月的所有星期一条目、第一个月的所有星期二条目、第一个月的所有星期三条目等的平均值。然后在接下来的几个月里做同样的事情。输出将是这样的:
KEY(Day of week per month) | VALUE(Avg of all values for the day of week on the month)
------------------------------------------------------------------------------------------
Month1Sunday | 23423
Month1Monday | 6452
Month1Tuesday | 94402
...
Month2Sunday | 342755
Month2Monday | 10923
这是我到目前为止所得到的:
private void getDailyBase(Map<Date, Double> hourlyPrices){
Calendar cal = Calendar.getInstance();
int month = 0;
boolean firstIteration = true;
int counter = 0;
Double acumValue =0D;
Double result;
Map<Integer, Double> averageAccumulator = new LinkedHashMap<Integer, Double>();
for (Map.Entry<Date, Double> entry : hourlyPrices.entrySet()){
cal.setTime(entry.getKey());
if (firstIteration){
month = cal.get(Calendar.MONTH);
firstIteration = false;
}
if (cal.get(Calendar.MONTH) == month){
acumValue += entry.getValue();
counter++;
}else {
result = acumValue/counter;
month = cal.get(Calendar.MONTH);
averageAccumulator.put(month, result);
counter = 0;
acumValue = 0D;
}
}
}
我可以累积每个月的值并将它们平均。但是当我试图找出或构建以星期为基础的结构时,我只是不知道如何去做或从哪里开始。我尝试过不同的迭代结构,但我想我没有看到全貌。
我正在使用 Java 6。