I am trying to write a recursive program that can print all permutations of a list of integers taken from a file, I am using HashMap to store list, where the keys are the numbers and the values are the number of times the key occurs in the list. The code is given below:
import java.util.*;
import java.io.*;
public class perm
{
public static void permute(HashMap<Integer, Integer> map)
{
if(map.isEmpty())
{
System.out.println();
}
else
{
Set<Integer> keys = map.keySet();
for(int num: keys)
{
System.out.print(Integer.toString(num)+",");
int val = map.get(num) -1;
if(val == 0)
{
map.remove(num);
}
else
{
map.put(num, val);
}
permute(map);
}
}
}
public static void main(String args []) throws FileNotFoundException
{
HashMap <Integer, Integer> map = new HashMap<Integer, Integer>();
Scanner in = new Scanner(new File(args[0]));
int num;
while(in.hasNextInt())
{
num = in.nextInt();
if(map.containsKey(num))
{
map.put(num, map.get(num)+1);
}
else
{
map.put(num,1);
}
}
permute(map);
}
}
I am getting a concurrency error, but I don't understand why, the error is given below:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
at perm.permute(perm.java:15)
at perm.permute(perm.java:27)
at perm.permute(perm.java:27)
at perm.permute(perm.java:27)
at perm.main(perm.java:48)
--------------------Code Edited based on comments --------------------
import java.util.*;
import java.io.*;
public class perm
{
public static void permute(HashMap<Integer, Integer> map)
{
if(map.isEmpty())
{
System.out.println();
}
else
{
Iterator it = map.entrySet().iterator();
//Set<Integer> keys = map.keySet();
while(it.hasNext())
//for(int num: keys)
{
Map.Entry pair = (Map.Entry)it.next();
int num = (int) pair.getKey();
System.out.print(Integer.toString(num)+",");
int val = (int) pair.getValue()-1;
//HashMap<Integer, Integer> newMap = map;
//int val = map.get(num) -1;
if(val == 0)
{
//newMap.remove(num);
it.remove();
}
else
{
//newMap.put(num, val);
pair.setValue(val);
}
permute(map);
}
}
}
public static void main(String args []) throws FileNotFoundException
{
HashMap <Integer, Integer> map = new HashMap<Integer, Integer>();
Scanner in = new Scanner(new File(args[0]));
int num;
while(in.hasNextInt())
{
num = in.nextInt();
if(map.containsKey(num))
{
map.put(num, map.get(num)+1);
}
else
{
map.put(num,1);
}
}
permute(map);
}
}
The same error still persists. I then went back to the initial approach and stored a temporary copy of the HashMap I was trying to iterate over and modified only the temporary copy and not the original one, the same error persists.
---- Code edit based on Answer 1 ------
import java.util.*;
import java.io.*;
public class perm
{
public static void permute(HashMap<Integer, Integer> map)
{
if(map.isEmpty())
{
System.out.println();
}
else
{
//Iterator it = map.entrySet().iterator();
HashMap<Integer, Integer> newMap = map;
Set<Integer> keys = map.keySet();
//while(it.hasNext())
for(int num: keys)
{
//Map.Entry pair = (Map.Entry)it.next();
//int num = (int) pair.getKey();
System.out.print(Integer.toString(num)+",");
//int val = (int) pair.getValue()-1;
//HashMap<Integer, Integer> newMap = map;
int val = map.get(num) -1;
if(val == 0)
{
newMap.remove(num);
//it.remove();
}
else
{
newMap.put(num, val);
//pair.setValue(val);
}
permute(newMap);
}
}
}
public static void main(String args []) throws FileNotFoundException
{
HashMap <Integer, Integer> map = new HashMap<Integer, Integer>();
Scanner in = new Scanner(new File(args[0]));
int num;
while(in.hasNextInt())
{
num = in.nextInt();
if(map.containsKey(num))
{
map.put(num, map.get(num)+1);
}
else
{
map.put(num,1);
}
}
permute(map);
}
}