Android Question IndexOf with Map as Argument

lip

Active Member
Licensed User
I have a List of Maps (the result of parsing a JSON String).

The Maps represent the two elements of the Primary Key of a SQLITE table eg:-

List = (PKMap1, PKMap2, PKMap3, PKMap4)

Where
PKMap1: (ID=1, DeviceNo=6789)
PKMap2: (ID=2, DeviceNo=6789)
PKMap3: (ID=1, DeviceNo=7123)
PKMap4: (ID=2,DeviceNo=7123)


I want to check if another record exists in the List, say
PKMap5: (ID=3, DeviceNo=6789)

Q1) Can I use:-

if List.IndexOf(PKMap5) = -1 then
'UpLoad PKMap5

Q2) If so, will the Map have to be in the same order?
ie will (ID=3, DeviceNo=6789) Match (DeviceNo=6789, ID=3)

Q3) If this stands a chance of working then I guess I will have to be careful how the various bits are Dim'ed to make sure that I am comparing Maps, and not pointers to Maps?
 

LucaMs

Expert
Licensed User
B4X:
Dim m, m1 As Map
m.Initialize
m1.Initialize

Dim lst As List : lst.Initialize

m.Put(1, "one")
m.Put(2, "two")
lst.Add(m)

m1.Put(3, "three")
m1.Put(4, "four")
lst.Add(m1)

Log(lst.IndexOf(m1))

m1.Put("twenty", 6)

Log(lst.IndexOf(m1))
logs:
1
1
 

lip

Active Member
Licensed User
BUT...

m2.Put(2,"two")
m2.Put(1,"one")

Log(lst.IndexOf(m2) = -1

The Order in which the Map Elements are added to the Map seem to affect whether the Map is found in the List?
 

lip

Active Member
Licensed User
B4X:
Dim m, m1 As Map
m.Initialize
m1.Initialize

Dim lst As List : lst.Initialize

m.Put(1, "one")
m.Put(2, "two")
lst.Add(m)

m1.Put(3, "three")
m1.Put(4, "four")
lst.Add(m1)

Log(lst.IndexOf(m1))

m1.Put("twenty", 6)

Log(lst.IndexOf(m1))
logs:
1
1

BUT...

m2.Put(2,"two")
m2.Put(1,"one")

Log(lst.IndexOf(m2) = -1

The Order in which the Map Elements are added to the Map seem to affect whether the Map is found in the List?
 

LucaMs

Expert
Licensed User
BUT...

m2.Put(2,"two")
m2.Put(1,"one")

Log(lst.IndexOf(m2) = -1

The Order in which the Map Elements are added to the Map seem to affect whether the Map is found in the List?

Have you forgotten to add m2 to the List?
B4X:
Dim m, m1, m2 As Map
m.Initialize
m1.Initialize
m2.Initialize

Dim lst As List : lst.Initialize

m.Put(1, "one")
m.Put(2, "two")
lst.Add(m)

m1.Put(3, "three")
m1.Put(4, "four")
lst.Add(m1)

Log("index of m1:" & lst.IndexOf(m1))

m1.Put("twenty", 6)

Log("index of m1:" & lst.IndexOf(m1))

m2.Put(2,"two")
m2.Put(1,"one")
lst.Add(m2)

Log("index of m2:" & lst.IndexOf(m2))
 
Top