I am really stumped by this. I have a map collection and I am trying to selectively delete items from the map using "map.remove". I keep on getting a "java.util.NoSuchElementException".
I can reproduced the problem using a very simple example code snippet shown below:
Here is the Log. As you can see it looks like keys 1,3 and 5 are deleted but then the code crashes after that.
I realize that I can use "m.clear" to accomplish the same result but that's not what I want to do. I want to selectively remove items from the map based on some condition with the possibility of ALL items being removed:
For Each s As String In m.Keys
if <some condition is true> then m.Remove(s) <--- could possibly be true for the entire list
Next
I also tried a traditional For-Next loop with the same results.
Question is ... does anyone know what is wrong with the code? And if it cannot be done this way, how would one delete selected items (and potentially ALL items) in a map collection using a loop and without having to use "map.clear"
I can reproduced the problem using a very simple example code snippet shown below:
B4X:
Sub AppStart (Args() As String)
Dim m As Map
m.Initialize
m.Put("1","XXX")
m.Put("2","XX")
m.Put("3","X")
m.Put("4","XXXX")
m.Put("5","XXXX")
For Each s As String In m.Keys
log(s)
m.Remove(s)
Next
Log(m.Size)
End Sub
Here is the Log. As you can see it looks like keys 1,3 and 5 are deleted but then the code crashes after that.
B4X:
1
3
5
main._appstart (java line: 70)
java.util.NoSuchElementException
at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:721)
at java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:752)
at java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:750)
at anywheresoftware.b4a.objects.collections.Map$MyMap.getEntry(Map.java:219)
at anywheresoftware.b4a.objects.collections.Map$MyMap.getKey(Map.java:196)
at anywheresoftware.b4a.objects.collections.Map.GetKeyAt(Map.java:95)
at anywheresoftware.b4a.objects.collections.Map$IterableMap.Get(Map.java:160)
at b4j.example.main._appstart(main.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at b4j.example.main.main(main.java:29)
I realize that I can use "m.clear" to accomplish the same result but that's not what I want to do. I want to selectively remove items from the map based on some condition with the possibility of ALL items being removed:
For Each s As String In m.Keys
if <some condition is true> then m.Remove(s) <--- could possibly be true for the entire list
Next
I also tried a traditional For-Next loop with the same results.
Question is ... does anyone know what is wrong with the code? And if it cannot be done this way, how would one delete selected items (and potentially ALL items) in a map collection using a loop and without having to use "map.clear"