B4J Question GetValueAt deprecated

MrKim

Well-Known Member
Licensed User
Longtime User
If this is deprecated how do you get the last item in the list without iterating through the whole list?

1591004766620.png
 

DonManfred

Expert
Licensed User
Longtime User
how do you get the last item in the list without iterating through the whole list?
You are not using any List in your Code. You are using a MAP.

You can get the last item from a List with
B4X:
dim last as int = mylist.Size-1
dim item as Object = mylist.Get(last)
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
You are not using any List in your Code. You are using a MAP.

You can get the last item from a List with
B4X:
dim last as int = mylist.Size-1
dim item as Object = mylist.Get(last)
I know I am using a Map. I just upgraded to 8.3 and now I get the deprecated message above.
As I understand it, .GET requires the key, not the index. I don't have the key, I just want to get the last item without iterating though the whole list.
What am I not understanding?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
may you should be using a list, if you don't mind the suggestion.

the problem with maps, in general, is that the order in which the items were entered is not maintained. so, technically, the "last" item in a map may not "be the droid item you're looking for". in other words, you have no idea what the last item in a map is because of the way a map is structured. this may or may not be a problem for you. if you just want the "last" item in the map - regardless of what it is or when it was added to the map - and you don't have its key, then you have little choice but to find out how big the map is and to last for the last item:

B4X:
' warning: deprecated code follows
dim count as int = map.size
dim value as string = map.getValueAt( count - 1)
' out of curiosity, if you wanted to get the last key while you're at it, you can add:
dim thekey as string = map.getKeyAt( count - 1)

if you used a list, the order would be maintained, and the last item in the list would be the most recently added. if that mattered to you.

all that said, i believe i read somewhere here that, in B4A, order is maintained. nevertheless, in this instance, since you don't know the key, you really have no choice but to acess the "last" item in a map with the code suggested here. stepping through all the entries to get to the last one, serves no particular purpose.

if you want to know why getkeyat() and getvalueat() are deprecated, you'll have to ask a higher authority.
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
may you should be using a list, if you don't mind the suggestion.

the problem with maps, in general, is that the order in which the items were entered is not maintained. so, technically, the "last" item in a map may not "be the droid item you're looking for". in other words, you have no idea what the last item in a map is because of the way a map is structured. this may or may not be a problem for you. if you just want the "last" item in the map - regardless of what it is or when it was added to the map - and you don't have its key, then you have little choice but to find out how big the map is and to last for the last item:

B4X:
' warning: deprecated code follows
dim count as int = map.size
dim value as string = map.getValueAt( count - 1)
' out of curiosity, if you wanted to get the last key while you're at it, you can add:
dim thekey as string = map.getKeyAt( count - 1)

if you used a list, the order would be maintained, and the last item in the list would be the most recently added. if that mattered to you.

all that said, i believe i read somewhere here that, in B4A, order is maintained. nevertheless, in this instance, since you don't know the key, you really have no choice but to acess the "last" item in a map with the code suggested here. stepping through all the entries to get to the last one, serves no particular purpose.

if you want to know why getkeyat() and getvalueat() are deprecated, you'll have to ask a higher authority.
Again, you are showing me deprecated code. The question is, if GetKeyAt and GetValueAt are going away. How do you get the last item without iterating the entire Map.

Given that many years ago I was told that maps are a good thing and there are suggestions that they are faster I have used a lot of them, Also, they have a Key, not just an index so VERY useful. Now I am told that two key functions are deprecated. This would cause a lot of rewrite.
You should switch to B4XOrderedMap if you want to access items based on the index.

B4X:
Dim key As String = OrderedMap.Keys.Get(7)
Dim value As Object = OrderedMap.Get(key)
Is it as fast (B4XOrderedMap)? The only reason I get the Last value is for speed. I have done this a number of places. I create a map that never changes. In this case it is a reference to a number of B4XViews. If the last item is out of spec then I iterate the entire list and make changes. Otherwise I Return.
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Accessing items based on index is faster with B4XOrderedMap.
The standard map needs to iterate over all values when you call GetKeyAt or GetValueAt.
My bad, I wasn't clear. My concern is also with adding items as well as looking them up. A number of maps get created so that needs to be fast, They don't all necessarily get used, but if they do it is for resizing based on resizing the form which must be quick or there is a lot of flicker.
 
Upvote 0
Top