Question

I have difficulties in understanding the following error.

Suppose you have a class A in which I implement the following method:

Map<Double,Integer> get_friends(double user){

Map<Double,Integer> friends = user_to_user.row(user);
//friends.putAll(user_to_user.column(user));

return friends;}

Then in the main I do the following:

A obj = new A();
Map<Double,Integer> temp = obj.get_friends(6);

Well this works fine. However when I uncomment the follwing line in class A:

friends.putAll(user_to_user.column(user));

and I run again the program, it crashes and throws me the concurrentModificationException. It is to be noted, that I am creating the Table user_to_user as follows:

private HashBasedTable<Double,Double,Integer> user_to_user;//
user_to_user = HashBasedTable.create();

What is further surprising is that when I interchange the way I am filling friends, I mean in that way:

Map<Double,Integer> friends = user_to_user.column(user);
friends.putAll(user_to_user.row(user));

Then everyting will work fine. Any idea ?

Was it helpful?

Solution

The issue is that HashBasedTable is internally implemented as a Map<Double, Map<Double, Integer>>, and that the implementation of user_to_user.column(user) is iterating over the rows at the same time you're modifying the row associated with user.

One workable alternative would be to copy user_to_user.column(user) into a separate Map before putting it into the row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top