code:
Map<Integer,DealCountUpdater> dealCountMap=new HashMap<Integer,DealCountUpdater>();
public void update(){
for(Map.Entry<Integer, DealCountUpdater> e:new HashMap<Integer,DealCountUpdater>(dealCountMap).entrySet()){//line:58
System.out.println(e.hashCode());
}
}
when I run this code,get below exception:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
at java.util.HashMap$EntryIterator.next(HashMap.java:834)
at java.util.HashMap$EntryIterator.next(HashMap.java:832)
at java.util.HashMap.putAllForCreate(HashMap.java:435)
at java.util.HashMap.<init>(HashMap.java:225)
at org.my.tuan.count.CountUpdater.update(CountUpdater.java:58)
at org.my.tuan._Maintainer.run(TuanSched.java:110)
this line is CountUpdater.java:58 :
for(Map.Entry<Integer, DealCountUpdater> e:new HashMap<Integer,DealCountUpdater>(dealCountMap).entrySet()){
I google this program,I know I can using a ConcurrentHashMap instead of a plain HashMap,
but I want to know ,why I using :
new HashMap<Integer,DealCountUpdater>(dealCountMap)
to create new Instance for HashMap,still throw ConcurrentModificationException ?
how to fix it by not using ConcurrentHashMap ?
thanks for help :)
//updating.... We aren't psychic. Please add the code to the post. Also, is your program multi-threaded, and is there other activity that could alter the source map?for(Map.Entry<Integer, DealCountUpdater> e:new HashMap<Integer,DealCountUpdater>(dealCountMap).entrySet()){, I added it in my question,//updating...is nothing to do