我打電話函數返回TreeMap
實例,而在調用代碼中我想修改TreeMap
。但是,我得到ConcurrentModificationException
。避免TreeMap ConcurrentModificationException?
這裏是我的代碼:
public Map<String, String> function1() {
Map<String, String> key_values = Collections.synchronizedMap(new TreeMap<String, String>());
// all key_values.put() goes here
return key_values;
}
而且我調用的代碼是:
Map<String, String> key_values =Collections.synchronizedMap(Classname.function1());
//here key_values.put() giving ConcurrentModificationException
我可以問一下從function1內創建一個同步映射嗎?它不被任何人使用,除了你的「呼叫代碼」... –
你如何修改地圖? –