从hashmap中删除元素时的java.util.ConcurrentModificationException
我正在学习HashMap类,并编写了这个简单的程序。 此代码适用于向hashmap添加元素,在从hashmap中删除元素时,我遇到了从hashmap中删除元素时的java.util.ConcurrentModificationException,java,hashmap,Java,Hashmap,我正在学习HashMap类,并编写了这个简单的程序。 此代码适用于向hashmap添加元素,在从hashmap中删除元素时,我遇到了java.util.ConcurrentModificationException 例如,这里有一份我的终端 [ravi@doom test]$ java TestHashMap .....MENU..... 1. Add 2. remove key 3. remove value 4. display 7. Exit Your choice :1 Key :
java.util.ConcurrentModificationException
例如,这里有一份我的终端
[ravi@doom test]$ java TestHashMap
.....MENU.....
1. Add
2. remove key
3. remove value
4. display
7. Exit
Your choice :1
Key : A
Value : 1
Key/Value : (A,1) added to storage.
.....MENU.....
1. Add
2. remove key
3. remove value
4. display
7. Exit
Your choice :1
Key : B
Value : 2
Key/Value : (B,2) added to storage.
.....MENU.....
1. Add
2. remove key
3. remove value
4. display
7. Exit
Your choice :1
Key : C
Value : 3
Key/Value : (C,3) added to storage.
.....MENU.....
1. Add
2. remove key
3. remove value
4. display
7. Exit
Your choice :1
Key : D
Value : 4
Key/Value : (D,4) added to storage.
.....MENU.....
1. Add
2. remove key
3. remove value
4. display
7. Exit
Your choice :4
( D , 4 );
( A , 1 );
( B , 2 );
( C , 3 );
.....MENU.....
1. Add
2. remove key
3. remove value
4. display
7. Exit
Your choice :2
Key to REMOVE :
D
Pair (D,4) Removed.
.....MENU.....
1. Add
2. remove key
3. remove value
4. display
7. Exit
Your choice :4
( A , 1 );
( B , 2 );
( C , 3 );
.....MENU.....
1. Add
2. remove key
3. remove value
4. display
7. Exit
Your choice :3
Enter Value to remove : 2
Key : B Removed.
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:922)
at java.util.HashMap$EntryIterator.next(HashMap.java:962)
at java.util.HashMap$EntryIterator.next(HashMap.java:960)
at TestHashMap.start(TestHashMap.java:60)
at TestHashMap.main(TestHashMap.java:87)
ABRT problem creation: 'success'
代码:
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 choice :");
}
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 to storage.");
break;
case 2:
System.out.println("Key to REMOVE : ");
input.nextLine();
key = input.nextLine();
Integer v = map.get(key);
if(v == null)
System.out.println("No value exists for key "+key);
else
{
map.remove(key);
System.out.println("Pair ("+key+","+v.intValue()+") Removed.");
}
break;
case 3:
System.out.print("Enter Value to 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 Choice !");
}
}
}
public static void main(String args[])
{
TestHashMap thm = new TestHashMap();
thm.start();
}
}
import java.util.HashMap;
导入java.util.Map;
导入java.util.Scanner;
类TestHashMap
{
私人地图;
公共TestHashMap()
{
map=新的HashMap();
}
公共void显示菜单()
{
System.out.println(“…菜单…”);
System.out.println(“1.Add”);
System.out.println(“2.移除键”);
System.out.println(“3.删除值”);
System.out.println(“4.display”);
System.out.println(“7.Exit”);
System.out.print(“您的选择:”);
}
公开作废开始()
{
扫描仪输入=新扫描仪(System.in);
国际货币基金组织,价值;
字符串键;
while(true)
{
显示菜单();
menuChoice=input.nextInt();
开关(menuChoice)
{
案例1:
系统输出打印(“\n键:”);
input.nextLine();
key=input.nextLine();
系统输出打印(“\n值:”);
value=input.nextInt();
put(键,新整数(值));
System.out.println(“键/值:(“+Key+”,“+Value+”)添加到存储中。”);
打破
案例2:
System.out.println(“要删除的键:”);
input.nextLine();
key=input.nextLine();
整数v=map.get(key);
如果(v==null)
System.out.println(“键”+键不存在值);
其他的
{
地图。删除(键);
System.out.println(“已删除对(“+key+”,“+v.intValue()+”));
}
打破
案例3:
System.out.print(“输入要删除的值:”);
value=input.nextInt();
if(map.containsValue(新整数(值)))
{
对于(Map.Entry:Map.entrySet())
{
if(entry.getValue().intValue()==value)
{
System.out.println(“Key:+entry.getKey()+“Removed”);
map.remove(entry.getKey());
}
}
}
打破
案例4:
对于(Map.Entry:Map.entrySet())
{
System.out.println(“(“+entry.getKey()+”,“+entry.getValue()+”;”);
}
打破
案例7:
input.close();
系统出口(0);
违约:
System.out.println(“无效选择!”);
}
}
}
公共静态void main(字符串参数[])
{
TestHashMap thm=新的TestHashMap();
thm.start();
}
}
更新:工作代码
多亏了你们两位(杰特曼、内森·休斯)
import java.util.HashMap;
导入java.util.Map;
导入java.util.Scanner;
导入java.util.Iterator;
类TestHashMap
{
私人地图;
公共TestHashMap()
{
map=新的HashMap();
}
公共void显示菜单()
{
System.out.println(“…菜单…”);
System.out.println(“1.Add”);
System.out.println(“2.移除键”);
System.out.println(“3.删除值”);
System.out.println(“4.display”);
System.out.println(“7.Exit”);
System.out.print(“您的选择:”);
}
公开作废开始()
{
扫描仪输入=新扫描仪(System.in);
国际货币基金组织,价值;
字符串键;
while(true)
{
显示菜单();
menuChoice=input.nextInt();
开关(menuChoice)
{
案例1:
系统输出打印(“\n键:”);
input.nextLine();
key=input.nextLine();
系统输出打印(“\n值:”);
value=input.nextInt();
put(键,新整数(值));
System.out.println(“键/值:(“+Key+”,“+Value+”)添加到存储中。”);
打破
案例2:
System.out.println(“要删除的键:”);
input.nextLine();
key=input.nextLine();
整数v=map.get(key);
如果(v==null)
System.out.println(“键”+键不存在值);
其他的
{
地图。删除(键);
System.out.println(“已删除对(“+key+”,“+v.intValue()+”));
}
打破
案例3:
System.out.print(“输入要删除的值:”);
value=input.nextInt();
if(map.containsValue(新整数(值)))
{
for(Iterator it=map.entrySet().Iterator();it.hasNext();)
{
Map.Entry x=it.next();
if(x.getValue().intValue()==value)
{
key=x.getKey();
it.remove();
System.out.println(“键:“+Key+”已删除”);
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 choice :");
}
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 to storage.");
break;
case 2:
System.out.println("Key to REMOVE : ");
input.nextLine();
key = input.nextLine();
Integer v = map.get(key);
if(v == null)
System.out.println("No value exists for key "+key);
else
{
map.remove(key);
System.out.println("Pair ("+key+","+v.intValue()+") Removed.");
}
break;
case 3:
System.out.print("Enter Value to remove : ");
value = input.nextInt();
if(map.containsValue(new Integer(value)))
{
for (Iterator<Map.Entry<String, Integer>> it = 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 Choice !");
}
}
}
public static void main(String args[])
{
TestHashMap thm = new TestHashMap();
thm.start();
}
}
for (Iterator<Map.Entry<String, Integer> it = map.entrySet().iterator();
it.hasNext();) {
for(Map.Entry<String,Integer> entry : map.entrySet() )
Iterator<Map.Entry<String, Integer>> itr = map.entrySet().iterator();
while(itr.hasNext())
{
Map.Entry<String, Integer> entry = itr.next();
if(entry.getValue().intValue() == 2)
{
System.out.println("Key : "+entry.getKey()+" Removed.");
itr.remove(); // Call Iterator's remove method.
}
}
case 3:
System.out.print("Enter Value to remove : ");
value = input.nextInt();
if(map.containsValue(new Integer(value)))
{
Map.Entry<String,Integer> foo = null;
for(Map.Entry<String,Integer> entry : map.entrySet() )
if(entry.getValue().intValue() == value)
foo = entry;
System.out.println("Key : "+foo.getKey()+" Removed.");
map.remove(foo.getKey());
}
break;
for (Iterator<Map.Entry<String, Integer> it = map.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<String, Integer> e= itr.next();
key = e.getKey();
Value = e.getValue();
//Your Other Code Here
it.remove(); //It removes the current Itertaion from Map
}