Android Question Looking for a MapList

Widget

Well-Known Member
Licensed User
Longtime User
I want to create a list (non-GUI) that is searchable (and fast) using something like Get("abc"), but retains its order so I can traverse the list in the same order as the items that were added.

A map would almost work except the order of items added is not retained.
A list would almost work except if I want to look something up, the List.IndexOf looks for the entire entry. In other words, if I want to add a Key and Value then I would need a custom Type for the entry, but then I have to search on the entire custom type (including the value) and since I am trying to get the value, this is not going to work.

Example:
B4X:
Private Listx as ListMap

ListX.initialize
ListX.Add("A", "Testing 1 2 3")
ListX.Add("X", "abcd")
ListX.Add("B", "bbbb")

if ListX.IndexOf("X") >= 0 then
  'Do something
end if

for i=0 to ListX.Size-1
  private locValue as String
  locValue = ListX.Get(i)
  log(locValue)
next

It is almost as if I need to write my own class MapList that uses both a List (to retain the order of the items added) and a Map so I can retrieve the value for a given key.

Is there another solution?
TIA
 

Misterbates

Active Member
Licensed User
Use a map to store the key/value pairs, and add an entry to an accompanying list each time you add the entry to the map, with the list entry being the key? Only problem I can see is if you have multiple entries with the same key. You could wrap the whole thing in a class...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
If the order is important. Use a List and write some helper-methods which you need....

Edit: Sorry, did not read carefully :-/

Is there another solution?
I don´t see a better way than using a list with maps/or better custom types and some helper-methods. Sort the list by type-value for ex.
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
Ok, thanks guys. I wasn't sure if I overlooked something. I will create a class MapList.:rolleyes:
 
Upvote 0

Misterbates

Active Member
Licensed User
So guess what, I have a project that needs a MapList - something that can find by Key, that preserves the order of items added (so far a regular Map works well) ... and that allows items to be removed/re-inserted at specific positions (corresponding to user drag/drop to reorder).

I've spent a couple hours today building a custom class that implements all of the Map and List methods except a few that don't make sense for a MapList. I also can't figure out how to return an IterableList (.Keys and .Values).

I'm willing to share if you're still looking?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I'm no longer so sure that maps keep the order of entries.

I have a websocket server (B4J) which sends a map to a client (B4A) and the order of the entries is not preserved; unless the problem is the Runfunction method of the websocket ... but I do not think so.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I'm no longer so sure that maps keep the order of entries.

I have a websocket server (B4J) which sends a map to a client (B4A) and the order of the entries is not preserved; unless the problem is the Runfunction method of the websocket ... but I do not think so.
Order is not guaranteed with maps under Android, whatever method you use to read them.
From the Android API documentation: "This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time."


P.S.
I checked the B4A map implementation after writing my post and I realized that I was wrong. The Java class is LinkedHashMap, not HashMap, so the order is guaranteed (identical to the insertion order). Sorry for the wrong information.


Anyway, I get that "error".
 
Last edited:
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
Unfortunately I'm away for a couple of weeks but will be glad to offer any Class suggestions when I get back.

(There are never enough days in an hour to get things done.o_O)
 
Upvote 0

Misterbates

Active Member
Licensed User
Unfortunately I'm away for a couple of weeks but will be glad to offer any Class suggestions when I get back.

No worries. I wasn't wanting to step on your toes but needed the class, so I've gone ahead and written one anyway and uploaded as a library. I'd be happy to receive any suggestions you might have when you're able. :D
 
Upvote 0
Top