Can we add/modify an element while iterating through a HashMap. As per my knowledge, we can either get an enumerator or iterator on a HashMap. An iterator provides only remove() method, while an Enumerator returns elements in read-only mode.
Is there any way out if we need to change or add a new element into it while iterating or can we get a listiterator on a HashMap.
The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException.
You cannot get a ListIterator for a HashMap, because ListIterator is for lists, and a map is not a list.
Find a different strategy for solving the problem; for example, first collect all the elements that you want to add to the map in a different map, and then when you're done iterating, use putAll() on the original HashMap to put the new elements in the map.