如何从循环外部获取每个循环的Long计数值

时间:2017-08-17 05:26:13

标签: java hibernate java-server

crashService:

            for (CrashReportForChartForm crashReport : crashReportForChartForms) {
                //i want to get this count value and send it to else part below 
                Long count = crashReport.getCount();
                Date newDate = new SimpleDateFormat("MM/dd/yyyy").parse(crashReport.getAddedDate());
                dates.add(newDate);
            }

            for (LocalDate date = startDate; date.isBefore(endDate); date = date.plusDays(1)) {
                // convert local date to date format
                Date accdate = java.sql.Date.valueOf(date);

                DateTimeFormatter format = DateTimeFormatter.ofPattern("MM/dd/yyyy");
                String addDate = date.format(format);
                if (!dates.contains(accdate)) {
                    resultCrashReportForChartForms.add(new CrashReportForChartForm(addDate, new Long(0)));
                } else {
                    //count value should be get from count in crashReport
                    resultCrashReportForChartForms.add(newCrashReportForChartForm(addDate, count));
                }
            }
        }
    }
    return resultCrashReportForChartForms; 
}

2 个答案:

答案 0 :(得分:1)

Map<Date,Integer> crashCounts = new HashMap<>();
long count;
for (CrashReportForChartForm crashReport : crashReportForChartForms) {
     Date newDate = new SimpleDateFormat("MM/dd/yyyy").parse(crashReport.getAddedDate());
     if(crashCounts.containsKey(newDate)){
     count=crashCounts.get(newDate)+ crashReport.getCount();
     crashCounts.put(newDate,count);
   }
    else{
       crashCounts.put(newDate,crashReport.getCount());
    }
}

这将为您提供特定日期的崩溃列表。发布此内容后,您可以运行for循环来检查特定日期是否包含崩溃。如果没有崩溃,您可以添加包含崩溃计数0的虚拟记录。

答案 1 :(得分:1)

使用Map<Date,Long>

            Map<Date,Long> dateCount = new HashMap<>();
            for (CrashReportForChartForm crashReport : crashReportForChartForms) {
                //i want to get this count value and send it to else part below 
                Long count = crashReport.getCount();
                Date newDate = new SimpleDateFormat("MM/dd/yyyy").parse(crashReport.getAddedDate());
                dateCount.put(newDate, count);
            }

            for (LocalDate date = startDate; date.isBefore(endDate); date = date.plusDays(1)) {
                // convert local date to date format
                Date accdate = java.sql.Date.valueOf(date);

                DateTimeFormatter format = DateTimeFormatter.ofPattern("MM/dd/yyyy");
                String addDate = date.format(format);
                Long count = dateCount.get(accdate);
                if (count == null) {
                    resultCrashReportForChartForms.add(new CrashReportForChartForm(addDate, new Long(0)));
                } else {
                    //count value should be get from count in crashReport
                    resultCrashReportForChartForms.add(newCrashReportForChartForm(addDate, count));
                }
            }
        }
    }
    return resultCrashReportForChartForms; 
}

更新:

累积每个日期的计数:

            Map<Date,Long> dateCount = new HashMap<>();
            for (CrashReportForChartForm crashReport : crashReportForChartForms) {
                //i want to get this count value and send it to else part below 
                Date newDate = new SimpleDateFormat("MM/dd/yyyy").parse(crashReport.getAddedDate());
                Long count = dateCount.get(newDate);
                if (count == null) {
                    count = crashReport.getCount();
                } else {
                    count += crashReport.getCount();
                }
                dateCount.put(newDate, count);
            }

更新2:

请注意,在第二个循环中,accdate实际上是一个java.sql.Date,我不确定它是否可以用来从地图中检索计数。