-3

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.

kravi
  • 747
  • 1
  • 8
  • 13
  • Please share your code and point out where you get stucked. – Jan Zahradník Jun 11 '15 at 04:14
  • I'm not totally sure what you mean, but it looks like a duplicate of http://stackoverflow.com/questions/10993403/how-to-replace-hashmap-values-while-iterating-over-them-in-java – yshavit Jun 11 '15 at 04:16
  • Use a concurrent-hashmap instead of hashmap – CoderNeji Jun 11 '15 at 04:17
  • possible duplicate of [Iterate through a HashMap](http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap) – Jibby Jun 11 '15 at 04:46
  • If I interpreted what you mean by "updated inside its traversal loop" correctly, then this is a possible duplicate of [iterating over and removing from a map](http://stackoverflow.com/questions/1884889/iterating-over-and-removing-from-a-map) – Dennis Meng Jun 11 '15 at 05:41
  • I can't answer to you because the query is still [on hold]. But the idea is to create an “ArrayDequeue toDo”, when you insert into the map also insert into the queue. And do not iterate over map, extract from the queue until queue is empty. This is: linkscoll.put("ABC", "ABC"); toDo.add(“ABC”); while(!toDo.isEmpty()) { String url = toDo.poll(); System.out.println(url); linkscoll.put("DEF", "DEF"); toDo.add(“DEF”);}. And don't forget to check if an element is in the map before adding it to the queue if you want to avoid revisiting old elements. – acesargl Jun 12 '15 at 11:17
  • This query is not a duplicate of “iterating over and removing from a map” because kravi wants to add (not remove) in a traversal loop. And Iterator interface defines a “remove” method but not an “add” one. Only ListIterator interface include "add" semantic, but maps return sets (for traversal) not lists. – acesargl Jun 12 '15 at 11:29
  • @acesargl Really a good idea you gave me. And thanks to understand my query properly. – kravi Jun 12 '15 at 11:54

3 Answers3

2

I don't believe you can with a plain HashMap. It is possible to iterate a LinkedHashMap because it preserves insertion order. The linked LinkedHashMap Javadoc says (in part)

The iterators returned by the iterator method of the collections 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. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Try this:

for (Map.Entry<KeyObject, ValueObject> entry : yourHashMap.entrySet()){
    ValueObject thisObject = entry.getValue();
    ///Do stuff to thisObject
}
Jibby
  • 850
  • 1
  • 9
  • 21
0

You need to use iterator (java.util.Iterator) (See Here) But if you want to modify anything then you have to do it using the iterator only otherwise you will get concurrentModificationException.

Ouney
  • 1,164
  • 1
  • 10
  • 22