Android Question How to use IndexOf in a list of Map

Antonio D'Angeli

Member
Licensed User
i ave one list of map:
es:

lista:
map1: cod-00001, barcode-123456789, desc-pippo, cost-1
map2: cod-00002, barcode-123456780, desc-pluto, cost-1
ecc.......

the metod:
dim m as map
m.inizialize
For i1 = 0 To Lista.Size - 1
m = Lista.Get(i1)
value = m.Get("barcode")
If value ="123456789" Then
Trovato=True
Exit
End If
Next

is veri slow !!!!!

there is a similar method to the indexof as for the list?

thanks in advance !!!!!
 

OliverA

Expert
Licensed User
Longtime User
What is very slow? How many maps are in the list? Is "barcode" the only map entry you ever search for? Does the list order matter (does it matter if map2 is after map1 in the above example)?
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Antonio is just wrong the way you use the map.

If you use the map you do it to search quickly. If you use the List but that contains maps or type variables you can not use fast search with IndexOf, but you have to scan the whole list as you are doing now, wasting time.

Use the map in the correct way, entering the key field for the search and as a value a typed variable that contains the data.

see the example:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Type TyArt (Codice As String, Descrizione As String, Prezzo As Float)
   
    Dim M As Map
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    M.Initialize
   
    ' Populate Map
    For i=1 To 10
        Dim Articolo As TyArt
        Articolo.Initialize
        Articolo.Codice=$"Cod-$6.0(i)"$
        Articolo.Descrizione="Prodotto / Articolo"
        Articolo.Prezzo=Rnd(1,200)/Rnd(1,5)
       
        Dim Barcode As String = Rnd(100000,999999)
        M.Put(Barcode,Articolo)
    Next
   
    'ricerca
    If RicercaBarCode(Rnd(100000,999999))<>Null Then
        Log("esiste")
    Else
        Log("non esiste")
    End If
End Sub

Sub RicercaBarCode(BarCode As String) As TyArt
    If M.ContainsKey(BarCode) Then
        Return M.Get(BarCode)
    Else
        Return Null
    End If
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
If "barcode" is the only map entry you ever search for and the list order does not matter, then @LucaMs has the correct solution for your problem.
Edit: Same goes for @Star-Dust
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
for OliverA

i ave 800 maps on the list;
"barcode" is univocal, no sorted;
the list order is not important.
What @OliverA wanted to say is that "my solution" is valid but only if you have to search "always" by Barcode.

Cioè, usare una map di map come ti ho suggerito vale ovviamente nel caso in cui tu debba cercare-estrarre una map sempre e solo in base ad un dato, in questo caso il barcode.
 
Last edited:
Upvote 0

Antonio D'Angeli

Member
Licensed User
What @OliverA wanted to say is that "my solution" is valid but only if you have to search "always" by Barcode.

Cioè, usare una map di map come ti ho suggerito vale ovviamente nel caso in cui tu debba cercare-estrarre una map sempre e solo in base ad un dato, in questo caso il barcode.

posso creare l'ambiente così come mi suggerisci.
grazie ancora

p.s.: caffè pagato a te ed a Star-Dust
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Che caffè?
 
Upvote 0
Top