java.util.ConcurrentModificationException when removing elements from a hashmap -
java.util.ConcurrentModificationException when removing elements from a hashmap -
i learning hashmap
class , wrote simple program. code works adding elements hashmap , while removing elements hashmap , encountering java.util.concurrentmodificationexception
illustration here re-create of terminal,
[ravi@doom test]$ java testhashmap .....menu..... 1. add together 2. remove key 3. remove value 4. display 7. exit selection :1 key : value : 1 key/value : (a,1) added storage. .....menu..... 1. add together 2. remove key 3. remove value 4. display 7. exit selection :1 key : b value : 2 key/value : (b,2) added storage. .....menu..... 1. add together 2. remove key 3. remove value 4. display 7. exit selection :1 key : c value : 3 key/value : (c,3) added storage. .....menu..... 1. add together 2. remove key 3. remove value 4. display 7. exit selection :1 key : d value : 4 key/value : (d,4) added storage. .....menu..... 1. add together 2. remove key 3. remove value 4. display 7. exit selection :4 ( d , 4 ); ( , 1 ); ( b , 2 ); ( c , 3 ); .....menu..... 1. add together 2. remove key 3. remove value 4. display 7. exit selection :2 key remove : d pair (d,4) removed. .....menu..... 1. add together 2. remove key 3. remove value 4. display 7. exit selection :4 ( , 1 ); ( b , 2 ); ( c , 3 ); .....menu..... 1. add together 2. remove key 3. remove value 4. display 7. exit selection :3 come in value remove : 2 key : b removed. exception in thread "main" java.util.concurrentmodificationexception @ java.util.hashmap$hashiterator.nextentry(hashmap.java:922) @ java.util.hashmap$entryiterator.next(hashmap.java:962) @ java.util.hashmap$entryiterator.next(hashmap.java:960) @ testhashmap.start(testhashmap.java:60) @ testhashmap.main(testhashmap.java:87) abrt problem creation: 'success'
code:
import java.util.hashmap; import java.util.map; import java.util.scanner; class testhashmap { private map<string,integer> map; public testhashmap() { map = new hashmap<string,integer>(); } public void displaymenu() { system.out.println(".....menu....."); system.out.println("1. add"); system.out.println("2. remove key"); system.out.println("3. remove value"); system.out.println("4. display"); system.out.println("7. exit"); system.out.print("your selection :"); } public void start() { scanner input = new scanner(system.in); int menuchoice,value; string key; while(true) { displaymenu(); menuchoice = input.nextint(); switch(menuchoice) { case 1: system.out.print("\n key : "); input.nextline(); key = input.nextline(); system.out.print("\n value : "); value = input.nextint(); map.put(key,new integer(value)); system.out.println("key/value : ("+key+","+value+") added storage."); break; case 2: system.out.println("key remove : "); input.nextline(); key = input.nextline(); integer v = map.get(key); if(v == null) system.out.println("no value exists key "+key); else { map.remove(key); system.out.println("pair ("+key+","+v.intvalue()+") removed."); } break; case 3: system.out.print("enter value remove : "); value = input.nextint(); if(map.containsvalue(new integer(value))) { for(map.entry<string,integer> entry : map.entryset() ) { if(entry.getvalue().intvalue() == value) { system.out.println("key : "+entry.getkey()+" removed."); map.remove(entry.getkey()); } } } break; case 4: for(map.entry<string,integer> entry : map.entryset() ) { system.out.println("( "+entry.getkey()+" , "+entry.getvalue()+" );"); } break; case 7: input.close(); system.exit(0); default: system.out.println("invalid selection !"); } } } public static void main(string args[]) { testhashmap thm = new testhashmap(); thm.start(); } }
update: working code
thanks both (rgettman,nathan hughes) of you.
import java.util.hashmap; import java.util.map; import java.util.scanner; import java.util.iterator; class testhashmap { private map<string,integer> map; public testhashmap() { map = new hashmap<string,integer>(); } public void displaymenu() { system.out.println(".....menu....."); system.out.println("1. add"); system.out.println("2. remove key"); system.out.println("3. remove value"); system.out.println("4. display"); system.out.println("7. exit"); system.out.print("your selection :"); } public void start() { scanner input = new scanner(system.in); int menuchoice,value; string key; while(true) { displaymenu(); menuchoice = input.nextint(); switch(menuchoice) { case 1: system.out.print("\n key : "); input.nextline(); key = input.nextline(); system.out.print("\n value : "); value = input.nextint(); map.put(key,new integer(value)); system.out.println("key/value : ("+key+","+value+") added storage."); break; case 2: system.out.println("key remove : "); input.nextline(); key = input.nextline(); integer v = map.get(key); if(v == null) system.out.println("no value exists key "+key); else { map.remove(key); system.out.println("pair ("+key+","+v.intvalue()+") removed."); } break; case 3: system.out.print("enter value remove : "); value = input.nextint(); if(map.containsvalue(new integer(value))) { (iterator<map.entry<string, integer>> = map.entryset().iterator();it.hasnext();) { map.entry<string,integer> x = it.next(); if(x.getvalue().intvalue() == value) { key = x.getkey(); it.remove(); system.out.println("key : "+key+" removed."); } } } break; case 4: for(map.entry<string,integer> entry : map.entryset() ) { system.out.println("( "+entry.getkey()+" , "+entry.getvalue()+" );"); } break; case 7: input.close(); system.exit(0); default: system.out.println("invalid selection !"); } } } public static void main(string args[]) { testhashmap thm = new testhashmap(); thm.start(); } }
your for-loop gets map.entryset , uses iterator on work through map entries (this version of for-loop requires iterable, gets iterator iterable). when using iterator on map, remove things map without using iterator, concurrentmodificationexception. map telling iterator it's out of date.
you can write loop using iterator explicitly, this:
for (iterator<map.entry<string, integer> = map.entryset().iterator(); it.hasnext();) {
and utilize iterator's remove method when need delete entry.
java hashmap
Comments
Post a Comment