B4J Question merge 2 maps

le_toubib

Active Member
Licensed User
Longtime User
hi all
i use this code to merge map person2 into map person:
B4X:
            For Each k As String In person2.Keys
                Person.Put(k,person2.Get(k))
            Next
but for some reason not all key/values are inserted .. many are just missing in the debug popup.. what am i doing wrong?
 

DonManfred

Expert
Licensed User
Longtime User
You need to show us a project which shoes the problem.

This works for me

B4X:
    Dim m1 As Map
    m1.Initialize
    m1.Put("Key1", "Value1")
    m1.Put("Key2", "Value2")
    m1.Put("Key3", "Value3")
    Log(m1)

    Dim m2 As Map
    m2.Initialize
    m2.Put("Key4", "Value4")
    m2.Put("Key5", "Value5")
    m2.Put("Key6", "Value6")
    Log(m2)
    For Each k As String In m2.Keys
        m1.Put(k,m2.Get(k))
    Next
    Log(m1)

** Activity (main) Create, isFirst = true **
(MyMap) {Key1=Value1, Key2=Value2, Key3=Value3}
(MyMap) {Key4=Value4, Key5=Value5, Key6=Value6}
(MyMap) {Key1=Value1, Key2=Value2, Key3=Value3, Key4=Value4, Key5=Value5, Key6=Value6}
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
ok , thanks DON
when i added this to extend debug :
File.WriteMap(File.dirapp,"1.txt" , Person)
i found it full
seems like the debug popup limit was blocking the rest
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Building on @DonManfred's code and this (https://www.b4x.com/android/forum/t...java-util-map-to-a-b4a-map.70214/#post-445846) from @Erel:

B4X:
Dim m1 As Map
m1.Initialize
m1.Put("Key1", "Value1")
m1.Put("Key2", "Value2")
m1.Put("Key3", "Value3")
Log(m1)

Dim m2 As Map
m2.Initialize
m2.Put("Key4", "Value4")
m2.Put("Key5", "Value5")
m2.Put("Key6", "Value6")
Log(m2)
'   For Each k As String In m2.Keys
'       m1.Put(k,m2.Get(k))
'   Next
JavaMapMerge(m1, m2)
Log(m1)
B4X:
Sub JavaMapMerge(map1 As Map, map2 As Map)
   Dim m1 As JavaObject = map1
   Dim m2 As JavaObject = map2
   m1.RunMethod("putAll", Array(m2))
End Sub
Waiting for debugger to connect...
Program started.
(MyMap) {Key1=Value1, Key2=Value2, Key3=Value3}
(MyMap) {Key4=Value4, Key5=Value5, Key6=Value6}
(MyMap) {Key1=Value1, Key2=Value2, Key3=Value3, Key4=Value4, Key5=Value5, Key6=Value6}
Program terminated (StartMessageLoop was not called).
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
In fact, I am not sure if it would be a plus or not. For example with a simple Map

For Each took: 12 ms
JavaMapMerge took: 32 ms

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
 #CommandLineArgs:
 #MergeLibraries: True
#End Region
Sub Process_Globals
 
End Sub
Sub AppStart (Args() As String)
 Dim Person, Person2 As Map
 Person.Initialize
 Person2.Initialize
 
 For x=0 To 100
  Person2.Put("m" & x,Rnd(0,x+1))
 Next
 
 Dim Start As Long = DateTime.Now
 For Each K As String In Person2.Keys
  Person.Put(K,Person2.Get(K))
 Next
 PrintTookTime(Start,"For Each")

 Person.Clear
 Start = DateTime.now
 JavaMapMerge(Person,Person2)
 PrintTookTime(Start,"JavaMapMerge")
 
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub

Sub JavaMapMerge(map1 As Map, map2 As Map)
 Dim m1 As JavaObject = map1
 Dim m2 As JavaObject = map2
 m1.RunMethod("putAll", Array(m2))
End Sub

Sub PrintTookTime(Start As Long, MethodName As String)
 Log($"${MethodName} took: ${DateTime.Now-Start} ms"$)
End Sub

Edit : JavaMapMerge seems to be quicker after 130000 lines
for example with 149000 lines
For Each took: 172 ms
JavaMapMerge took: 107 ms
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Good catch! BTW, subsequent calls (even after re-dimming variables) to the same method (be it for/next or PutAll) can be faster (it's actually very roller coaster like).
JavaMapMerge took: 46 ms
JavaMapMerge took: 9 ms
JavaMapMerge took: 4 ms
JavaMapMerge took: 8 ms
JavaMapMerge took: 5 ms
JavaMapMerge took: 3 ms
JavaMapMerge took: 6 ms
JavaMapMerge took: 4 ms
JavaMapMerge took: 7 ms
JavaMapMerge took: 5 ms
For Each took: 38 ms
For Each took: 10 ms
For Each took: 18 ms
For Each took: 13 ms
For Each took: 21 ms
For Each took: 11 ms
For Each took: 14 ms
For Each took: 9 ms
For Each took: 10 ms
For Each took: 17 ms
That's processing 100000 map items.
 
Upvote 0
Top