【问题标题】:Modifying the class level hashmap changes reflecting in local reference修改反映在本地参考中的类级别哈希图更改
【发布时间】:2015-07-06 07:45:41
【问题描述】:

我正在尝试根据 insuranceid 将保险详细信息保存到数据库中,或者如果数据库中已经存在 insuranceid 则进行更新。问题是,当我尝试保存多个保险详细信息时,它会给出 ConcurrentModificationException。在调试时,我发现第一个周期可以,第一个保险详细信息已成功保存到数据库。但是当下一个保险细节被存储时,它给出了例外。另一件事是,这里我正在修改主 HashMap(patientId,HashMap(insuranceId,InsuranceInfo)),并且更改反映在本地引用中,即 id 在当前方法 () 中添加到 insuranceMap 变量。请尝试帮助我

这是我的存储方式...

public void storeInsuranceInformationToDataBase(int patientId) throws SQLException
{
    //It gives arrayList of isuranceIds which are already in the data base.
    ArrayList<Integer> insuranceIdsListInDatabase = getInsuranceIdsListInDatabase();  

    //It returns HashMap of insurance ids for given patient id. 
    HashMap<Integer, InsuranceInfo> insuranceMap = getInsuranceDetails(patientId);   
    Set<Integer> insuranceIdsInHashMap = insuranceMap.keySet();

    //Here I am getting ConcurrentModificationException
    for (int insuranceIdInHashMap : insuranceIdsInHashMap)
    {
        //Here I am comparing the both ids..
        if (insuranceIdsListInDatabase.contains(insuranceIdInHashMap))  
        {
            String updateInsuranceQuery = "UPDATE jp_InsuranceInfo SET  typeOfPolicy='" + insuranceMap.get(insuranceIdInHashMap).getTypeOfPolicy() + "', amount=" + insuranceMap.get(insuranceIdInHashMap).getAmount() + ", duration=" + insuranceMap.get(insuranceIdInHashMap).getDuration()
                    + ", companyName='" + insuranceMap.get(insuranceIdInHashMap).getCompanyName() + "' WHERE insuranceId=" + insuranceIdInHashMap + ";";

            getDataBase().getStatement().executeUpdate(updateInsuranceQuery);
        }
        else
        {
            String insertInsuranceQuery = "INSERT INTO jp_InsuranceInfo VALUES(" + insuranceMap.get(insuranceIdInHashMap).getPatientId() + ",'" + insuranceMap.get(insuranceIdInHashMap).getTypeOfPolicy() + "'," + insuranceMap.get(insuranceIdInHashMap).getAmount() + ","
                    + insuranceMap.get(insuranceIdInHashMap).getDuration() + ",'" + insuranceMap.get(insuranceIdInHashMap).getCompanyName() + "')";

            getDataBase().getStatement().executeUpdate(insertInsuranceQuery);

            ResultSet generatedInsuranceId = getDataBase().getStatement().executeQuery("SELECT SCOPE_IDENTITY()");
            int newInsuranceId = 0;
            if (generatedInsuranceId.next())
            {
                newInsuranceId = generatedInsuranceId.getInt(1);
            }

            //Here it returns an insurance object which contains insurance details
            InsuranceInfo insuranceObject = insuranceMap.get(insuranceIdInHashMap); 
            insuranceObject.setInsuranceId(newInsuranceId);
            //Here I am trying to store the newly inserted insurance object into main hashmap
            storeInsuranceInfoToHashMap(patientId, insuranceObject);

            //I am removing previous insurance object with temporary insurance id
            getInsuranceMap().get(patientId).remove(insuranceIdInHashMap);

            // Adding newly generated insurance id to array list
            getInsuranceIdsListInDatabase().add(newInsuranceId);
        }
    }

【问题讨论】:

  • 没有。实际上,在提到的链接中,他正在尝试修改相同的哈希图,同时对其进行迭代。但我正在修改主哈希图。变化反映在本地哈希映射中。
  • 好的," 但是,当存储下一个保险详细信息时,它给出了异常" - 异常可能是什么? storeInsuranceInfoToHashMap 是做什么的?你应该倾向于使用PreparedStatements,更多细节请看Using Prepared Statements
  • 其实我用的是一张桌子。首先,我将保险详细信息添加到表格中。并且相同的细节正在添加到 hashmap 中(看起来像问题)。这里的哈希映射就像缓存。然后我将 Hash Map 中的保险详细信息添加到数据库中。
  • 这并不能说明错误。 getInsuranceMap().get(patientId).remove 看起来也很可疑
  • 它的'ConcurrentModificationException'。不。实际上在“storeInsuranceInfoToHashMap()”方法中,我正在添加新的保险对象。完成此操作后,新保险 ID 会自动在“Set insuranceIdsInHashMap”中更新。这是我正在迭代的 SET。这是获得 ConcurrentModificationException 的主要原因。我不明白这是怎么回事。

标签: java hashmap set


【解决方案1】:

嗯,没什么奇怪的。您在迭代时尝试将新项目添加到 Map 对象中。

我的建议是尝试将您在遍历 Map 时创建的保险数据保存到临时的 Map 对象中。并且,在您完成对 Map 的迭代之后。然后,您可以将临时地图保存到初始地图中。

示例:

public void storeInsuranceInformationToDataBase(int patientId) throws SQLException
{
    //It gives arrayList of isuranceIds which are already in the data base.
    ArrayList<Integer> insuranceIdsListInDatabase = getInsuranceIdsListInDatabase();  

    //It returns HashMap of insurance ids for given patient id. 
    HashMap<Integer, InsuranceInfo> insuranceMap = getInsuranceDetails(patientId);   
    Map<Integer, InsuranceInfo> tempInsuranceMap = new HashMap<>();
    Set<Integer> insuranceIdsInHashMap = insuranceMap.keySet();

    //Here I am getting ConcurrentModificationException
    for (int insuranceIdInHashMap : insuranceIdsInHashMap)
    {
        //Here I am comparing the both ids..
        if (insuranceIdsListInDatabase.contains(insuranceIdInHashMap))  
        {
            String updateInsuranceQuery = "UPDATE jp_InsuranceInfo SET  typeOfPolicy='" + insuranceMap.get(insuranceIdInHashMap).getTypeOfPolicy() + "', amount=" + insuranceMap.get(insuranceIdInHashMap).getAmount() + ", duration=" + insuranceMap.get(insuranceIdInHashMap).getDuration()
                    + ", companyName='" + insuranceMap.get(insuranceIdInHashMap).getCompanyName() + "' WHERE insuranceId=" + insuranceIdInHashMap + ";";

            getDataBase().getStatement().executeUpdate(updateInsuranceQuery);
        }
        else
        {
            String insertInsuranceQuery = "INSERT INTO jp_InsuranceInfo VALUES(" + insuranceMap.get(insuranceIdInHashMap).getPatientId() + ",'" + insuranceMap.get(insuranceIdInHashMap).getTypeOfPolicy() + "'," + insuranceMap.get(insuranceIdInHashMap).getAmount() + ","
                    + insuranceMap.get(insuranceIdInHashMap).getDuration() + ",'" + insuranceMap.get(insuranceIdInHashMap).getCompanyName() + "')";

            getDataBase().getStatement().executeUpdate(insertInsuranceQuery);

            ResultSet generatedInsuranceId = getDataBase().getStatement().executeQuery("SELECT SCOPE_IDENTITY()");
            int newInsuranceId = 0;
            if (generatedInsuranceId.next())
            {
                newInsuranceId = generatedInsuranceId.getInt(1);
            }

            //Here it returns an insurance object which contains insurance details
            InsuranceInfo insuranceObject = insuranceMap.get(insuranceIdInHashMap); 
            insuranceObject.setInsuranceId(newInsuranceId);
            //Here I am trying to store the newly inserted insurance object into main hashmap
            storeInsuranceInfoToHashMap(patientId, insuranceObject); <-- make this function to save those information into the temporary map

            //I am removing previous insurance object with temporary insurance id
            getInsuranceMap().get(patientId).remove(insuranceIdInHashMap);

            // Adding newly generated insurance id to array list
            getInsuranceIdsListInDatabase().add(newInsuranceId);
        }
    }
    insuranceMap.putAll(tempInsuranceMap);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 2013-01-01
    相关资源
    最近更新 更多