elasticsearch,hashmap,Java,elasticsearch,Hashmap" /> elasticsearch,hashmap,Java,elasticsearch,Hashmap" />

Java Hashmap值被覆盖

Java Hashmap值被覆盖,java,elasticsearch,hashmap,Java,elasticsearch,Hashmap,我将我的Hashmap作为类中的全局变量: private Map<String, CodaReportDTO> dateAndDTO = new TreeMap<>(); //hashmap for date and the dto hashmap添加部分位于上述代码段的底部: dateAndDTO.put(dateAndMonthFinal,codaReportDTO) 上一行中的键和值正确。但当它尝试更新新键的值时,它会用新值替换具有值的键的其他值。我哪里做错了

我将我的
Hashmap
作为类中的全局变量:

private Map<String, CodaReportDTO> dateAndDTO = new TreeMap<>(); //hashmap for date and the dto
hashmap添加部分位于上述代码段的底部:

dateAndDTO.put(dateAndMonthFinal,codaReportDTO)

上一行中的键和值正确。但当它尝试更新新键的值时,它会用新值替换具有值的键的其他值。我哪里做错了


在研究了SO中的其他问题后,我已经尝试了一整天,但仍然无法通过。任何帮助都将不胜感激

您正在对所有地图条目使用相同的DTO实例。这就是为什么每次更改都会反映在所有值上。如果不希望覆盖更改,则需要为每个映射键创建一个新实例。

映射的使用方式存在问题。如果要对同一个键使用多个值,则应使用List作为
Map

的第二个参数。它将替换具有新值的其他键的其他值
我不确定是否理解该部分。请您详细解释一下到底发生了什么?codaReportDTO的实例化在哪里?是否所有值都使用一个实例?这将解释symptoms@aviad我已经在这个类本身中全局地进行了实例化:
private CodaReportDTO CodaReportDTO=new CodaReportDTO()确定,所以您使用的是一个DTO实例,所有值都指向它。每次更改它时,显然更改都会反映在其他项上。如果不希望覆盖更改,则需要为每个新键创建DTO的新实例
private JSONObject postDataToElasticSearchForSuccessCount(String url, String operatorID) throws IOException, JSONException, ParseException {

    /*JSONObject jsonObject = elasticSearchDataReceiver.getResults(url);

    //getting the number of hits
    JSONObject totalHits = jsonObject.getJSONObject("hits");
    Object hitsCountForSuccessCount = totalHits.get("total");
    String hitCountForSuccessCount = hitsCountForSuccessCount.toString();

    if (Integer.parseInt(hitCountForSuccessCount) > 0) {
        codaReportDTO.setNumOfTxn(Integer.parseInt(hitCountForSuccessCount));
    }
    return new JSONObject(jsonObject.toString());*/

    JSONObject updatedJsonObject = null;
    String jsonBody;
    String monthName = Month.of(Integer.parseInt(excelMonth)).name();
    int numberOfDaysInAMonth = Utilities.getNumberOfDaysForMonth(Integer.parseInt(excelYear), monthName);

    //iterate over the month
    for (int i = 1; i < numberOfDaysInAMonth; i++) {


        String day = appendZeroToDay(i);
        excelDateMonth = day + "-" + excelMonth;
        Date excelMonthOriginal = new SimpleDateFormat("dd-MM").parse(excelDateMonth);
        String formattedDate = String.valueOf(excelMonthOriginal);
        String MonthOnly = formattedDate.substring(4, 7);
        String DateOnly = formattedDate.substring(8, 10);
        String dateAndMonthFinal = DateOnly + "-" + MonthOnly;
        excelDateMonth = dateAndMonthFinal;

        String input = "{  \n" +
                "   \"query\":{  \n" +
                "      \"query_string\":{  \n" +
                "         \"query\":\"api:\\\"smsmessaging\\\" AND operatorid:" + operatorID + " AND transactionOperationStatus:\\\"\\\" AND responsecode:(200 201) AND year:" + excelYear + " AND month:" + excelMonth + " AND day:" + day + "\"\n" +
                "      }\n" +
                "   },\n" +
                "   \"aggs\":{  \n" +
                "      \"total\":{  \n" +
                "         \"terms\":{  \n" +
                "            \"field\":\"userid\"\n" +
                "         },\n" +
                "         \"aggs\":{  \n" +
                "            \"grades_count\":{  \n" +
                "               \"value_count\":{  \n" +
                "                  \"script\":\"doc['userid'].value\"\n" +
                "               }\n" +
                "            }\n" +
                "         }\n" +
                "      }\n" +
                "   }\n" +
                "}\n";

        jsonBody = input;
        JSONObject jsonObject = elasticSearchDataReceiver.getResult(url, jsonBody);

        JSONObject totalHits = jsonObject.getJSONObject("hits");
        Object hitsCountForSuccessCount = totalHits.get("total");
        String hitCountForSuccessCount = hitsCountForSuccessCount.toString();
        int hitCount = Integer.parseInt(hitCountForSuccessCount);

        if (hitCount > 0) {
            updatedJsonObject = jsonObject;
            codaReportDTO.setNumOfTxn(hitCount);
            dateAndDTO.put(dateAndMonthFinal, codaReportDTO);
        }
    }
    return updatedJsonObject;
}