Android Question Map.Remove key causing a crash

TurboCCC

Member
Licensed User
I'm trying to remove all keys that have the same value. All other instances of myMap.Remove(k) in my program work but this one with the FOR loop gets a crash on the first item it tries to remove. I believe this is related to the FOR loop but I could be wrong. The IF statement is well executed. It just crashes on the first loop when k = "Key1". I don't get it.

B4X:
Dim myMap As Map = CreateMap("Key1": "1", "Key2": "1")
    For Each k In myMap.Keys
        If (myMap.Get(k) = "1") Then
            myMap.Remove(k)
        End If
    Next

This is the error log:
B4X:
Error occurred on line: 98 (MapTest)
java.util.NoSuchElementException
    at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:759)
    at java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:790)
    at java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:788)
    at anywheresoftware.b4a.objects.collections.Map$MyMap.getEntry(Map.java:216)
    at anywheresoftware.b4a.objects.collections.Map$MyMap.getKey(Map.java:196)
    at anywheresoftware.b4a.objects.collections.Map.GetKeyAt(Map.java:93)
    at anywheresoftware.b4a.objects.collections.Map$1.Get(Map.java:136)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6702)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…