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

Java 如何从循环外部获取每个循环的长计数值,java,hibernate,java-server,Java,Hibernate,Java Server,速递服务: for (CrashReportForChartForm crashReport : crashReportForChartForms) { //i want to get this count value and send it to else part below Long count = crashReport.getCount(); Date newDat

速递服务:

            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; 
}
Map crashCounts=newhashmap();
长计数;
用于(CrashReportForChartForm crashReport:crashReportForChartForms){
Date newDate=new SimpleDateFormat(“MM/dd/yyyy”).parse(crashReport.getAddedDate());
if(容器数量(新日期)){
count=crashCounts.get(newDate)+crashReport.getCount();
crashCounts.put(新日期,计数);
}
否则{
crashCounts.put(newDate,crashReport.getCount());
}
}
这将为您提供特定日期的崩溃列表。发布此消息后,您可以运行for循环来检查特定日期是否包含崩溃。如果没有崩溃,您可以添加包含崩溃计数0的虚拟记录。

Map crashCounts=new HashMap();
长计数;
用于(CrashReportForChartForm crashReport:crashReportForChartForms){
Date newDate=new SimpleDateFormat(“MM/dd/yyyy”).parse(crashReport.getAddedDate());
if(容器数量(新日期)){
count=crashCounts.get(newDate)+crashReport.getCount();
crashCounts.put(新日期,计数);
}
否则{
crashCounts.put(newDate,crashReport.getCount());
}
}
这将为您提供特定日期的崩溃列表。发布此消息后,您可以运行for循环来检查特定日期是否包含崩溃。如果没有崩溃,您可以添加包含崩溃计数0的虚拟记录。

使用
映射:

Map dateCount=newhashmap();
用于(CrashReportForChartForm crashReport:crashReportForChartForms){
//我想得到这个计数值并将其发送到下面的其他部分
Long count=crashReport.getCount();
Date newDate=new SimpleDateFormat(“MM/dd/yyyy”).parse(crashReport.getAddedDate());
dateCount.put(newDate,count);
}
对于(LocalDate-date=startDate;date.isBefore(endDate);date=date.plusDays(1)){
//将本地日期转换为日期格式
Date accdate=java.sql.Date.valueOf(日期);
DateTimeFormatter format=模式的DateTimeFormatter.of(“MM/dd/yyyy”);
字符串addDate=date.format(格式);
Long count=dateCount.get(accdate);
如果(计数=null){
结果crashreportforchartforms.add(新CrashReportForChartForm(addDate,新Long(0));
}否则{
//计数值应从crashReport中的计数中获取
结果crashreportforchartforms.add(newCrashReportForChartForm(addDate,count));
}
}
}
}
返回ChartForms的结果rashreportforChartForms;
}
更新:

要累积每个日期的计数,请执行以下操作:

            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);
            }
Map dateCount=newhashmap();
用于(CrashReportForChartForm crashReport:crashReportForChartForms){
//我想得到这个计数值并将其发送到下面的其他部分
Date newDate=new SimpleDateFormat(“MM/dd/yyyy”).parse(crashReport.getAddedDate());
Long count=dateCount.get(newDate);
如果(计数=null){
count=crashReport.getCount();
}否则{
count+=crashReport.getCount();
}
dateCount.put(newDate,count);
}
更新2:

请注意,在第二个循环中,accdate实际上是一个java.sql.Date,我不确定它是否可以按原样从映射中检索计数。

使用
映射

Map dateCount=newhashmap();
用于(CrashReportForChartForm crashReport:crashReportForChartForms){
//我想得到这个计数值并将其发送到下面的其他部分
Long count=crashReport.getCount();
Date newDate=new SimpleDateFormat(“MM/dd/yyyy”).parse(crashReport.getAddedDate());
dateCount.put(newDate,count);
}
对于(LocalDate-date=startDate;date.isBefore(endDate);date=date.plusDays(1)){
//将本地日期转换为日期格式
Date accdate=java.sql.Date.valueOf(日期);
DateTimeFormatter format=模式的DateTimeFormatter.of(“MM/dd/yyyy”);
字符串addDate=date.format(格式);
Long count=dateCount.get(accdate);
如果(计数=null){
结果crashreportforchartforms.add(新CrashReportForChartForm(addDate,新Long(0));
}否则{
//计数值应从crashReport中的计数中获取
结果crashreportforchartforms.add(newCrashReportForChartForm(addDate,count));
}
}
}
}
返回ChartForms的结果rashreportforChartForms;
}
更新:

要累积每个日期的计数,请执行以下操作:

            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);
            }
Map dateCount=newhashmap();
用于(CrashReportForChartForm crashReport:crashReportForChartForms){
//我想得到这个计数值并将其发送到下面的其他部分
Date newDate=new SimpleDateFormat(“MM/dd/yyyy”).parse(crashReport.getAddedDate());
Long count=dateCount.get(newDate);
如果(计数=null){
count=crashReport.getCount();
}否则{
count+=crashReport.getCount();
}
dateCount.put(newDate,count);
}
更新2:


请注意,在第二个循环中,accdate实际上是一个java.sql.Date,我不确定它是否可以按原样从映射中检索计数。

只需在
for
循环的
之外声明
Long count
。这只是范围的问题(缩进隐藏了问题)。我只想从for每个循环中获取crashReport.getCount(),并将其发送到resultCrashReportForChartForms.add(newCrashReportForCha
            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);
            }