Android Question How to sum values and group them by a key value for List of Maps

jahswant

Well-Known Member
Licensed User
Longtime User
I have a List of Map:

B4X:
Dim list = List[
(MyMap) {"A"=10, "B"=5, "C"=3}
(MyMap) {"A"=10, "B"=5, "C"=3}
(MyMap) {"A"=10, "B"=5, "C"=3}
(MyMap) {"A"=10, "B"=5, "C"=3}
(MyMap) {"A"=10, "B"=5, "C"=3}
(MyMap) {"A"=10, "B"=5, "C"=3}
(MyMap) {"A"=10, "B"=5, "C"=3}
(MyMap) {"A"=10, "B"=5, "C"=3}
]

I want to sum the value and group them by key value in the most efficient way so it becomes:
C = 24, B = 40, A = 80
 
Solution
Here is the complete code combining Marco's and Angel's code. Both of whom get all the credit , not me:
B4X:
Dim listOfMaps As List
    listOfMaps.Initialize
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3", "D":"5", "E":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3", "E":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3","D":"5","E":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
  
    Dim MapUniqueKey As Map
    MapUniqueKey.Initialize
    For Each MapNumber...

MarcoRome

Expert
Licensed User
Longtime User
B4X:
Dim listOfMaps As List
    listOfMaps.Initialize
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
   
   
    Dim a,b,c As Int
    For i = 0 To listOfMaps.Size - 1
        a = a + listOfMaps.Get(0).As(Map).Get("A")
        b = b + listOfMaps.Get(0).As(Map).Get("B")
        c = c + listOfMaps.Get(0).As(Map).Get("C")
    Next
   
    Log($"C = ${c}, B = ${b}, A = ${a}"$)
 
Upvote 1

jahswant

Well-Known Member
Licensed User
Longtime User
Thanks for your quick answer it works correctly the issue is that most of the time I will not know the keys. I will start from here. Thanks again.

In that case the map may look like this :

B4X:
Dim list = List[
(MyMap) {"A"=10, "B"=5, "C"=3,"D"=5, "E"=3}
(MyMap) {"A"=10, "B"=5, "C"=3, "E"=3}
(MyMap) {"A"=10, "B"=5, "C"=3}
(MyMap) {"A"=10, "B"=5, "C"=3,"D"=5, "E"=3}
(MyMap) {"A"=10, "B"=5, "C"=3}
(MyMap) {"A"=10, "B"=5, "C"=3}
(MyMap) {"A"=10, "B"=5, "C"=3}
(MyMap) {"A"=10, "B"=5, "C"=3}
]

I want to sum the value and group them by key value in the most efficient way so it becomes:
C = 24, B = 40, A = 80, E = 9, D = 10
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
B4X:
Dim listOfMaps As List
    listOfMaps.Initialize
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3", "D":"5", "E":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3", "E":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3","D":"5","E":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
  
 
    Dim a,b,c,d,e As Int
    For i = 0 To listOfMaps.Size - 1
        For Each k As String In listOfMaps.Get(i).As(Map).Keys
            Select k
                Case "A"
                    a = a + listOfMaps.Get(i).As(Map).Get(k)
                Case "B"
                    b = b + listOfMaps.Get(i).As(Map).Get(k)
                Case "C"
                    c = c + listOfMaps.Get(i).As(Map).Get(k)
                Case "D"
                    d = d + listOfMaps.Get(i).As(Map).Get(k)
                Case "E"
                    e = e + listOfMaps.Get(i).As(Map).Get(k)
            End Select
        Next
    Next
  
    Log($"C = ${c}, B = ${b}, A = ${a}, E = ${e}, D = ${d}"$)
 
Upvote 1

angel_

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim MapUniqueKey As Map
    MapUniqueKey.Initialize
    For Each MapNumber As Map In listOfMaps
        For Each Key As String In MapNumber.Keys
            Dim Value As Double = MapNumber.Get(Key) + MapUniqueKey.GetDefault(Key, 0)
            MapUniqueKey.Put(Key, Value)           
        Next
    Next
    
    Log(MapUniqueKey)
 
Upvote 1

Mahares

Expert
Licensed User
Longtime User
Here is the complete code combining Marco's and Angel's code. Both of whom get all the credit , not me:
B4X:
Dim listOfMaps As List
    listOfMaps.Initialize
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3", "D":"5", "E":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3", "E":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3","D":"5","E":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
    listOfMaps.Add(CreateMap("A":"10", "B":"5", "C":"3"))
  
    Dim MapUniqueKey As Map
    MapUniqueKey.Initialize
    For Each MapNumber As Map In listOfMaps
        For Each Key As String In MapNumber.Keys
            Dim Value As Int = MapNumber.Get(Key) + MapUniqueKey.GetDefault(Key, 0)
            MapUniqueKey.Put(Key, Value)
        Next
    Next
    Dim sb As StringBuilder
    sb.Initialize
    For Each k As String In MapUniqueKey.Keys
        Log($"${k} = ${MapUniqueKey.Get(k)}"$)
        sb.Append($"${k} = ${MapUniqueKey.Get(k)}, "$)
    Next
    Dim s As String = sb.ToString
    s = s.SubString2(0, s.LastIndexOf(","))
    Log(s)  'A = 80, B = 40, C = 24, D = 10, E = 9
 
Upvote 0
Solution
Top