I need a data structure like HashMap
or any other Collection
, which will be updated during the traversal. I tried to traverse HashMap
, ConcurrentHashMap
and LinkedHashMap
using Iterator
, but I am not able get result. Snippet of the code is:
Map<String, String> linkscoll = new ConcurrentHashMap<String, String>();
linkscoll.put("ABC", "ABC");
for(Iterator it = linkscoll.entrySet().iterator(); it.hasNext(); )
{
Map.Entry entry = (Map.Entry)it.next();
String temp = (String) entry.getValue();
System.out.println(temp);
linkscoll.put("DEF", "DEF");
}
My output should be:
ABC
DEF
But it is giving output as ABC
I am really need of this. Please help me. Thanks.
I don't believe you can with a plain
HashMap
. It is possible to iterate aLinkedHashMap
because it preserves insertion order. The linkedLinkedHashMap
Javadoc says (in part)