B4J Question Remove key from Map

Blueforcer

Well-Known Member
Licensed User
Longtime User
I want to remove all keys from a map, wich doesnt exist in a list. But i cant get it to work. Whats wrong with my code?


B4X:
    Dim map1 As Map = CreateMap("test1":True,"test2":True,"test3":True)
    Dim list As List = Array As String("test1","test3")
   
    For Each appkey As String In map1.Keys
        If list.IndexOf(appkey) = -1 Then
            map1.Remove(appkey)
        End If
    Next


B4X:
Fehler in Zeile: 221
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 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.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at com.ab.abplugin.ABPlugin.CheckForNewPlugins(ABPlugin.java:209)
    at de.awtrix.main._displayready(main.java:1488)
    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.debug.Debug.CallSub4(Debug.java:115)
 
Last edited:

Kiffi

Well-Known Member
Licensed User
Longtime User
The map elements are rearranged by a Remove(). For this reason it is better to iterate from the last to the first element.

B4X:
Dim map1 As Map = CreateMap("test1":True,"test2":True,"test3":True)
Dim list As List = Array As String("test1","test3")

For Counter = map1.Size - 1 To 0 Step -1
        Dim appkey As String = map1.GetKeyAt(Counter)
        If list.IndexOf(appkey) = -1 Then
                map1.Remove(appkey)
        End If
Next
 
Upvote 0
Top