Java 修改反映在本地引用中的类级hashmap更改
我正在尝试根据insuranceid将保险详细信息保存到数据库中,或者如果数据库中已经存在insuranceid,则进行更新。问题是,当我试图保存多个保险详细信息时,它给出了ConcurrentModificationException。在调试期间,我发现第一个周期可以确定,第一个保险详细信息已成功保存到数据库。但当下一个保险细节被存储时,它给出了一个例外。另一件事是,在这里我修改了主HashMap(patientId,HashMap(insuranceId,InsuranceInfo)),这些更改反映在本地引用中,即id添加到当前方法()中的insuranceMap变量中。请试着帮助我 这是我的存储方法Java 修改反映在本地引用中的类级hashmap更改,java,hashmap,set,Java,Hashmap,Set,我正在尝试根据insuranceid将保险详细信息保存到数据库中,或者如果数据库中已经存在insuranceid,则进行更新。问题是,当我试图保存多个保险详细信息时,它给出了ConcurrentModificationException。在调试期间,我发现第一个周期可以确定,第一个保险详细信息已成功保存到数据库。但当下一个保险细节被存储时,它给出了一个例外。另一件事是,在这里我修改了主HashMap(patientId,HashMap(insuranceId,InsuranceInfo)),这些
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);
}
}
public void storeInsuranceInformationToDataBase(int patientId)引发SQLException
{
//它给出了数据库中已有的isuranceId的arrayList。
ArrayList InsuranceIDListIndatabase=GetInsuranceIDListIndatabase();
//它返回给定患者id的保险id的HashMap。
HashMap insuranceMap=getInsuranceDetails(patientId);
设置insuranceIdsInHashMap=insuranceMap.keySet();
//这里我得到了ConcurrentModificationException
对于(int insuranceIdInHashMap:insuranceIdInHashMap)
{
//这里我比较两个ID。。
if(保险IDListIndatabase.contains(保险IDHashMap))
{
String updateInsuranceQuery=“UPDATE jp_InsuranceInfo SET-typeOfPolicy=”“+insuranceMap.get(InsuranceIdHashMap.getTypeOfPolicy()+”,amount=“+insuranceMap.getAmount(InsuranceIdHashMap.getDuration)”,duration=“+insuranceMap.get(InsuranceIdHashMap).getDuration()
+“,companyName=”“+insuranceMap.get(InsuranceIdHashMap.getCompanyName()+”,其中insuranceId=“+InsuranceIdHashMap+”;”;
getDataBase().getStatement().executeUpdate(updateInsuranceQuery);
}
其他的
{
String insertInsuranceQuery=“插入jp_InsuranceInfo值(“+insuranceMap.get(InsuranceIdHashMap).getPatientId()+”,“+insuranceMap.get(InsuranceIdHashMap).get(InsuranceIdHashMap).getTypeOfPolicy()+”,“+insuranceMap.get(InsuranceIdHashMap).getAmount()+”,”
+获取(InsuranceIdHashMap.getDuration()+”,“+insuranceMap.get(InsuranceIdHashMap.getCompanyName()+”)”;
getDataBase().getStatement().executeUpdate(insertInsuranceQuery);
ResultSet GeneratedSuranceId=getDataBase().getStatement().executeQuery(“选择范围\标识()”);
int newInsuranceId=0;
if(generatedSuranceId.next())
{
newInsuranceId=generatedInsuranceId.getInt(1);
}
//在这里,它返回一个包含保险详细信息的保险对象
InsuranceInfo insuranceObject=insuranceMap.get(InsuranceIdHashMap);
insuranceObject.setInsuranceId(newInsuranceId);
//在这里,我试图将新插入的保险对象存储到主hashmap中
storeInsuranceInfoToHashMap(patientId,insuranceObject);
//我正在删除以前具有临时保险id的保险对象
getInsuranceMap().get(patientId).remove(InsuranceIdHashMap);
//将新生成的保险id添加到数组列表
GetInsuranceIDListIndatabase().add(newInsuranceId);
}
}
好吧,没什么奇怪的。您试图在迭代映射对象时将新项添加到该对象中
我的建议是,尝试将您在迭代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);
}
public void storeInsuranceInformationToDataBase(int patientId)引发SQLException
{
//它给出了数据库中已有的isuranceId的arrayList。
ArrayList InsuranceIDListIndatabase=GetInsuranceIDListIndatabase();
//它返回给定患者id的保险id的HashMap。
HashMap insuranceMap=getInsuranceDetails(patientId);
Map tempInsuranceMap=新HashMap();
设置insuranceIdsInHashMap=insuranceMap.keySet();
//这里我得到了ConcurrentModificationException
对于(int insuranceIdInHashMap:insuranceIdInHashMap)
{
//这里我比较两个ID。。
if(保险IDListIndatabase.contains(保险IDHashMap))
{
String updateInsuranceQuery=“UPDATE jp_InsuranceInfo SET-typeOfPolicy=”“+insuranceMap.get(InsuranceIdHashMap.getTypeOfPolicy()+”,amount=“+insuranceMap.getAmount(InsuranceIdHashMap.getDuration)”,duration=“+insuranceMap.get(InsuranceIdHashMap).getDuration()
+“,companyName=”“+insuranceMap.get(InsuranceIdHashMap.getCompanyName()+”,其中insuranceId=“+InsuranceIdHashMap+”;”;
getDataBase().getStatement().executeUpdate(updateInsuranceQuery);
}
其他的
{
String insertInsuranceQuery=“插入jp_InsuranceInfo值(“+insuranceMap.get(InsuranceIdHashMap).getPatientId()+”,“+insuranceMap.get(InsuranceIdHashMap).get(InsuranceIdHashMap).getTypeOfPolicy()+”,“+insuranceMap.get(InsuranceIdHashMap).getAmount()+”,”
+获取(InsuranceIdHashMap.getDuration()+”,“+insuranceMap.get(InsuranceIdHashMap.getCompanyName()+”)”;
getDataBase().getStatement().executeUpdate(insertInsuranceQuery);
ResultSet GeneratedSuranceId=getDataBase().getStatement().executeQuery(“选择范围\标识()”);
int newInsuranceId=0;
if(generatedSuranceId.next())
{
newInsuranceId=generatedInsuranceId.getInt(1);
}
//