Java Concurrent modification exception
•
Java
I wrote the following code, resulting in concurrent modification exceptions How can I prevent it? Our idea is to escape all the values of the map and rebuild the object (do) with the new param map
try {
Map<String,String[]> paramMap = dO.getParameterMap();
Set<Map.Entry<String,String[]>> entries = paramMap.entrySet();
Iterator<Map.Entry<String,String[]>> it = entries.iterator();
while (it.hasNext()) {
Map.Entry<String,String[]> entry = it.next();
String[] values = entry.getValue();
List<String> valList = new ArrayList<String>();
if (values != null) {
for (String value : values) {
valList.add(escapeHTML(value));
}
dO.removeParameter(entry.getKey());
//Please note that parameter is a HashMap, so do you need to delete the entry before inserting, otherwise it will replace the new value associated with the key How does it work in Java?
dO.addParameter(entry.getKey(),valList.toArray(new String[valList.size()]));
}
}
}
Solution
The exception is thrown because you add / delete the content in the map during iteration:
dO.removeParameter(entry.getKey()); dO.addParameter(entry.getKey(),valList.toArray(new String[valList.size()]
You should use iterator Remove() instead
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
