-1

So I have a TreeMap TreeMap<Integer, String> map = new TreeMap<>(); and lets say that it only contains this: mapa.put(i, "first"); (i = 0)

now I want to iterate and add elements to map but for some reason it will iterate just once although I have added more elements in for loop

for(Map.Entry<Integer, String> entry: mapa.entrySet()){
     mapa.put(i+1, ""+i);
     System.out.println(entry);

     if(i == 10){
         break;
     }
     i++;
    }

it will write out just this:

0=first

How can I iterate through map that can change size in for loop?

Thank you!

Petar
  • 1
  • 3
  • You are expanding your entrySet after you determined the number of runs it should do. You could use a "while(true){" list instead of your "for(Map.Entry...". But it seams that you want to achieve something like just filling a map. For this i would suggest to use a for loop with an incrementing index. – GJohannes Apr 10 '22 at 09:32
  • 1
    Does this answer your question? [Adding elements to a collection during iteration](https://stackoverflow.com/questions/993025/adding-elements-to-a-collection-during-iteration) – Joe Apr 10 '22 at 12:07

1 Answers1

1

From javadoc of entrySet method: "If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined.". You could use size() method of the map, but then be careful with for loop condition (don't produce endless loop). For example:

    Map<String, Integer> map = new TreeMap<>();
    map.put("-1", -1);
    for (int i = 0; i < map.size(); i++) {
        map.put("" + i, i);
        if (i == 10)
            break;
    }
Andrei Prakhov
  • 206
  • 1
  • 5
  • yes but that way I can't get values I want, for instance if I want to in the same time get values and put values in map, I cant iterate if I dont know what key is used – Petar Apr 10 '22 at 12:05
  • could you please give me a hint which goal you want to achieve? May be you need to redesign your code a bit. You can get entrySet inside the loop any time you want to get the values, but that wouldn't be good solution. – Andrei Prakhov Apr 10 '22 at 12:14
  • My goal is to have two ThreeMaps map1 and map2, for every element from map1 I check can I pair that element with any element from map2 ( by my criteria) If I can, I would like to add key from map1 and map2 and values from map1 and map2 in map1 – Petar Apr 10 '22 at 12:29
  • I Can go into better details if explanation above is not good enough – Petar Apr 10 '22 at 12:29
  • it would look like this – Petar Apr 10 '22 at 12:37
  • for(int j = 0; j < map1.size(); j++){ for(int k = 0; k < map2.size(); k++){ if(pair (value1, value2)){map1.put(map1.value + map1.key + map2.value + map2.key) } } } – Petar Apr 10 '22 at 12:39
  • Then you could use one more different map to put there pairing results and then add all at once to map1 when loop is finished. – Andrei Prakhov Apr 10 '22 at 12:46