-1
private List<String> duplicateParamList(Map<DistName, List<String>> totalHashMap, Map.Entry<String, String> param,
Map.Entry<DistName, ManagedObject> entry) {
List<String> duplicateList = new ArrayList<>();
if (totalHashMap.isEmpty()) {
List<String> values = new ArrayList<>();
values.add(param.getValue());
totalHashMap.put(entry.getKey(), values);
return duplicateList;
}
for (Map.Entry<DistName, List<String>> totalEntry : totalHashMap.entrySet()) {
if (totalEntry.getValue().contains(param.getValue())) {
duplicateList.add(param.getKey());
} else {
if (totalHashMap.containsKey(entry.getKey())) {
totalHashMap.get(entry.getKey()).add(param.getValue());
} else {
List<String> valueList = new ArrayList<>();
valueList.add(param.getValue());
totalHashMap.put(entry.getKey(), valueList);
}
}
}
return duplicateList;
}
它會拋出這個異常: java.lang.reflect.InvocationTargetException --- java.util.ConcurrentModificationException如何修復異常java.util.ConcurrentModificationException
如何解決這個問題呢?非常感謝。
這是我使用迭代器來代替了,但它也沒有有效:
Iterator<Map.Entry<DistName, List<String>>> iterator = totalHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<DistName, List<String>> totalEntry = iterator.next();
if (totalEntry.getValue().contains(param.getValue())) {
duplicateList.add(param.getKey());
} else {
if (totalHashMap.containsKey(entry.getKey())) {
totalHashMap.get(entry.getKey()).add(param.getValue());
} else {
List<String> valueList = new ArrayList<>();
valueList.add(param.getValue());
totalHashMap.put(entry.getKey(), valueList);
}
}
}
它不重複,因爲我的迭代器是totalHashMap,使用Iterator也沒有效果。 – danny
我用迭代器更新代碼 – danny