问题描述
我从用户处获取保险细节并将其保存在散列表中。
我有一个名为SAVE的按钮。所以,只有当用户点击这个按钮时,所有的保险应该保存在数据库
所以我采取随机生成的ID作为参考,直到我保存在数据库中的细节
保存在数据库后,我需要更新此使用key作为自动生成的id的hashmap
pre $ public void saveInformationInDatabase(int patientId)
{
// getAllInsurances returns HashMapn< Integer,HashMap< Integer,InsuranceInformation>>
Iterator< Map.Entry< Integer,InsuranceInformation>>保险= getAllInsurances()。get(patientId).entrySet()。iterator();
while(insurances.hasNext())
{
InsuranceInformation insuranceInformation = insurances.next()。getValue();
if(insuranceInformation.getStatus()== Status.OLD)
continue;
else if(insuranceInformation.getStatus()== Status.NEW)
{
//将信息保存在数据库中,并返回自动生成的ID
int licId = saveInformation(insuranceInformation );
//所以,我需要使用自动生成的ID
更新insuranceInformation //因为以前的ID是随机生成的数字
insuranceInformation.setLicID(licId);
insuranceInformation.setStatus(InsuranceObject.Status.OLD);
//下面的行给我ConcurrentModificationException
getAllInsurances()。get(patientId).put(licId,insuranceInformation); //使用新生成的id作为键存储更新后的信息,在散列表
insurances.remove(); //在这里,删除旧的HashMap条目
}
}
}
中的 c> Map
,当您试图迭代它时,这会导致异常,因为您在迭代它时无法修改集合。
相反,您应该使用第二个 Map
来存储更新的值,并使用 Map#putAll
来例如...重新同步...
//测试地图充满价值...
地图< Integer ,String> mapTest = new HashMap<>(25);
for(int index = 0; index< 10; index ++){
mapTest.put(index,Integer.toString(index));
}
//获取一个迭代器
Iterator< Map.Entry< Integer,String>> insurances = mapTest.entrySet()。iterator();
//为新值创建临时映射
Map< Integer,String> newValues = new HashMap<>(25);
while(insurances.hasNext()){
Map.Entry< Integer,String> entry = insurances.next();
int key = entry.getKey();
//比较我们想要做什么,这里
//我们甚至删除键
if(key%2 == 0){
// Remove旧条目
insurances.remove();
//使用临时图创建一个新条目
newValues.put(key * 10,entry.getValue());
}
}
//合并结果
mapTest.putAll(newValues);
作为一种可能的解决方案
I am taking the insurance details from user and saving them in a hashmap. And I have button called SAVE. So only, when user clicks on this button all insurances should save in Database So I am taking a random generated id as reference until I save the details in Database After saving in database, I need to update this hashmap with key as autogenerated id
public void saveInformationInDatabase(int patientId)
{
// getAllInsurances returns HashMapn<Integer, HashMap<Integer, InsuranceInformation>>
Iterator<Map.Entry<Integer, InsuranceInformation>> insurances = getAllInsurances().get(patientId).entrySet().iterator();
while(insurances.hasNext())
{
InsuranceInformation insuranceInformation = insurances.next().getValue();
if (insuranceInformation.getStatus() == Status.OLD)
continue;
else if (insuranceInformation.getStatus() == Status.NEW)
{
// Saving the Information in database, and returning auto generated ID
int licId = saveInformation(insuranceInformation);
// So, i need to update insuranceInformation with autogenerated ID
// Because previous id is randomly generated number
insuranceInformation.setLicID(licId);
insuranceInformation.setStatus(InsuranceObject.Status.OLD);
// Below line gives me ConcurrentModificationException
getAllInsurances().get(patientId).put(licId, insuranceInformation); // Storing the updated information with newly generated id as key, in hashmap
insurances.remove(); // and here, removing the old hashmap entry
}
}
}
解决方案 getAllInsurances().get(patientId).put(licId, insuranceInformation);
is updating the Map
while you're attempting to iterate it, this is causing the exception as you can't modify a collection while you're iterating it.
Instead, you should use a second Map
to store the updated values and the use Map#putAll
to resync the two, for example...
// Test map full of values...
Map<Integer, String> mapTest = new HashMap<>(25);
for (int index = 0; index < 10; index++) {
mapTest.put(index, Integer.toString(index));
}
// Grab an iterator
Iterator<Map.Entry<Integer, String>> insurances = mapTest.entrySet().iterator();
// Create a temp map for the new values
Map<Integer, String> newValues = new HashMap<>(25);
while(insurances.hasNext()) {
Map.Entry<Integer, String> entry = insurances.next();
int key = entry.getKey();
// Make the comparison about what we want to do, here
// we're removing even keys
if (key % 2 == 0) {
// Remove the old entry
insurances.remove();
// Use the temp map to create a new entry
newValues.put(key * 10, entry.getValue());
}
}
// Merge the results
mapTest.putAll(newValues);
As one possible solution
这篇关于HashMap中的ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!