I'm using a hashmap as an infinite map for a tile based game. I'm trying to access all values in a hashmap from another thread and changing the content at the same time. But I'm getting a java.util.ConcurrentModificationException. How can I do this without getting an exception? I'm getting the exception in this line:

Object[] objects = hashMap.values().toArray();

  • \$\begingroup\$ Is there a ConcurrentHashMap you can use instead? \$\endgroup\$ – Kurley Oct 18 '13 at 10:03
  • \$\begingroup\$ First thing you should do before asking a question like this is to Google the error message. You would have found plenty of answers at stackoverflow.com. \$\endgroup\$ – MichaelHouse Oct 18 '13 at 13:27

instead of using Collections.synchronizedMap you can use a java.util.ConcurrentMap implementation

I suggest using the hashmap implementation

ConcurrentMap<String, String> map = new ConcurrentHashMap<String, String>();

exposing the ConcurrentMap interface allows use of the test and update style atomic operation

  • \$\begingroup\$ When using a ConcurrentMap I'm getting many null pointer exceptions. Is there a way to convert a normal HashMap to a list or an array without getting the ConcurrentModificationException? \$\endgroup\$ – user1990950 Oct 18 '13 at 11:34
  • \$\begingroup\$ @user1990950 you may need to rethink your design, or add more checks \$\endgroup\$ – ratchet freak Oct 18 '13 at 11:54
  • \$\begingroup\$ Ok but is a ConcurrentMap efficient? \$\endgroup\$ – user1990950 Oct 18 '13 at 12:00
  • \$\begingroup\$ @user1990950 much more efficient than using Collections.synchronizedMap \$\endgroup\$ – ratchet freak Oct 18 '13 at 12:02
  • \$\begingroup\$ Ok I got rid of the null pointer exceptions, but it made my game completely unplayable. Is there an alternative to Hashmaps for infinite dynamic maps? \$\endgroup\$ – user1990950 Oct 18 '13 at 12:20

You get java.util.ConcurrentModificationException when you try to access the HashMap from more than one threads and at least one thread tries to modify it.

If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) Java HashMap

One way to solve it, is to wrap the Map using the Collections.synchronizedMap method. This way you will restrict the access to the HashMap only to one thread at a time ( call to a HashMap function) . Keep in mind that synchronizing the access to the map will make the calls a lot slower.

(e.x. of Collections.synchronizedMap)

Map<String, String> map = Collections.synchronizedMap(new HashMap<String, String>());
  • \$\begingroup\$ Is there a faster version? Just one thread modifies the values the other read them. \$\endgroup\$ – user1990950 Oct 18 '13 at 10:27
  • \$\begingroup\$ It depends on what you are trying to do. This is basically a multi-threading problem. If you write a few data at the map you could just stick with that implementation. Another solution is to use message-passing between the threads. There are a lot of different solutions, that each maps better at a different problem. \$\endgroup\$ – Anastasios G Oct 18 '13 at 10:46
  • \$\begingroup\$ Another way is to allow the first thread to finish writing all the data to the HashMap and then signals the second thread to begin reading. If it is allowed by your solution it would be fast and avoid synchronizedMap. \$\endgroup\$ – Anastasios G Oct 18 '13 at 10:50
  • \$\begingroup\$ Ok I've got a 2 threads that update the tiles, 1 thread that adds new tiles and drawing is done in the main thread, but I'm not converting the map into an array when drawing. \$\endgroup\$ – user1990950 Oct 18 '13 at 10:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.