修改反映在本地引用中的类级别hashmap更改

时间:2015-07-06 07:45:41

标签: java hashmap set

我正在尝试根据insuranceid将保险详细信息保存到数据库,或者如果数据库中已经存在insuranceid,则更新。问题是,当我尝试保存多个保险详细信息时,它会给出ConcurrentModificationException。调试时我发现第1个循环可以,第一个保险详细信息成功保存到数据库。但是当下一个保险细节存储时,它会给出例外情况。另一件事是,我在这里修改主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);
        }
    }

1 个答案:

答案 0 :(得分:1)

好吧,没什么奇怪的。您尝试在class myApp(App): def build(self): self.load_kv("kivy.3.kv") tb = ThemeBackground() print "value =",tb.myslider.value # <---- value here print "min =",tb.myslider.min # <--- min here return tb 对象中重复添加新项目。

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

示例:

Map