LucaMs Expert Licensed User Longtime User Dec 3, 2019 #1 Test project attached (its design also is strange, but it is similar to my real project). Error occurred on line: 37 (Main) Line 37 is an empty line. java.util.NoSuchElementException at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:759) Attachments MapTest.zip MapTest.zip 8 KB · Views: 565
Test project attached (its design also is strange, but it is similar to my real project). Error occurred on line: 37 (Main) Line 37 is an empty line. java.util.NoSuchElementException at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:759)
Erel B4X founder Staff member Licensed User Longtime User Dec 3, 2019 #2 Code: For Each Index As Int In mapX.Keys AddOrRemove(Index, False) Next You are modifying the map inside the For Each loop so the iterator tries to access an invalid item. You can call mapX.Clear instead of this code. Upvote 0
Code: For Each Index As Int In mapX.Keys AddOrRemove(Index, False) Next You are modifying the map inside the For Each loop so the iterator tries to access an invalid item. You can call mapX.Clear instead of this code.
LucaMs Expert Licensed User Longtime User Dec 3, 2019 #3 Erel said: You are modifying the map inside the For Each loop so the iterator tries to access an invalid item. Click to expand... Why? I'm using the map key. I could understand if I was using an array and a "for next" loop. Erel said: You can call mapX.Clear instead of this code. Click to expand... No, because I have to "handle" (GUI) every single removal. Upvote 0
Erel said: You are modifying the map inside the For Each loop so the iterator tries to access an invalid item. Click to expand... Why? I'm using the map key. I could understand if I was using an array and a "for next" loop. Erel said: You can call mapX.Clear instead of this code. Click to expand... No, because I have to "handle" (GUI) every single removal.
Erel B4X founder Staff member Licensed User Longtime User Dec 3, 2019 #4 LucaMs said: Why? I'm using the map key. I could understand if I was using an array and a "for next" loop. Click to expand... The iterator doesn't copy the list of keys so when you change the underlying data you are making it invalid. You need to make a copy yourself: Code: Dim KeysToRemove As List KeysToRemove.Initialize For Each Index As Int In mapX.Keys KeysToRemove.Add(Index) Next For Each k As Int In KeysToRemove mapX.Remove(k) Next Upvote 0
LucaMs said: Why? I'm using the map key. I could understand if I was using an array and a "for next" loop. Click to expand... The iterator doesn't copy the list of keys so when you change the underlying data you are making it invalid. You need to make a copy yourself: Code: Dim KeysToRemove As List KeysToRemove.Initialize For Each Index As Int In mapX.Keys KeysToRemove.Add(Index) Next For Each k As Int In KeysToRemove mapX.Remove(k) Next
LucaMs Expert Licensed User Longtime User Dec 3, 2019 #5 Erel said: The iterator doesn't copy the list of keys so when you change the underlying data you are making it invalid. Click to expand... Is it a java hashmap "behaviour" (I should say "bug")? Upvote 0
Erel said: The iterator doesn't copy the list of keys so when you change the underlying data you are making it invalid. Click to expand... Is it a java hashmap "behaviour" (I should say "bug")?
Erel B4X founder Staff member Licensed User Longtime User Dec 3, 2019 #6 It is not a bug. It is a designed limitation in favor of performance. It is not directly related to Java though HashMap native iterator will also fail if you modify the collection like this. Upvote 0
It is not a bug. It is a designed limitation in favor of performance. It is not directly related to Java though HashMap native iterator will also fail if you modify the collection like this.
LucaMs Expert Licensed User Longtime User Dec 3, 2019 #7 It sounds very strange to me. I would like to try with vb.net... if it didn't take about 8 hours just to start VS Thanks, Erel Upvote 0
It sounds very strange to me. I would like to try with vb.net... if it didn't take about 8 hours just to start VS Thanks, Erel
Erel B4X founder Staff member Licensed User Longtime User Dec 3, 2019 #8 BTW, if you switch to B4XOrderedMap from B4XCollections then you will be able to simplify it a bit: Code: Dim KeysToRemove As List KeysToRemove.Initialize KeysToRemove.AddAll(mapX.Keys) For Each Key As Int In KeysToRemove mapX.Remove(Key) Next It will also allow you to access elements based on an index, without using the keys for that: Code: mapX.Get(mapX.Keys.Get(3)) 'get the 4th element value Upvote 0
BTW, if you switch to B4XOrderedMap from B4XCollections then you will be able to simplify it a bit: Code: Dim KeysToRemove As List KeysToRemove.Initialize KeysToRemove.AddAll(mapX.Keys) For Each Key As Int In KeysToRemove mapX.Remove(Key) Next It will also allow you to access elements based on an index, without using the keys for that: Code: mapX.Get(mapX.Keys.Get(3)) 'get the 4th element value