addMapEntry to index 0

Post your questions and help other users.

Moderator: Martin

Post Reply
anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

addMapEntry to index 0

Post by anuraag » 21 Dec 2017 23:32

Is it possible to addMapEntry to index 0 that is beginning of map?

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: addMapEntry to index 0

Post by Desmanto » 22 Dec 2017 02:39

There is no direct function to do that. There is no also technical necessity to sort it too, since map values always work based on key-value mapping. So this is purely for aesthetic. I also wish to sort similar key together.

To insert at 0 index, the fastest way is using addAllMapEntries(). Create a temporary map of the key-value you wanna add, and add the original map to it.

Code: Select all

realmap = newMapFromValues(
"key1", "value1",
"key2", "value2");

addmap = newMapFromValues("key0", "value0"); // key value to be inserted
addAllMapEntries(addmap, realmap);

realmap = addmap;
removeVariable(addmap);
However it seems removeVariable(addmap) doesn't work properly in this 1.34.0 version. The addmap variable still shows up at debug dialog. Is it just me?

Sorting the map or inserting at index other than 0 can be done too, with extra effort. Even manual sorting is also possible, but more troublesome.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: addMapEntry to index 0

Post by bogdyro » 22 Dec 2017 14:45

This is the way to do it (include it in double quotes. Kind of annoying :)
removeVariable("addmap")

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: addMapEntry to index 0

Post by Desmanto » 22 Dec 2017 22:42

Thanks for the tip. :) I just use this removeVariable() the first time, even though i have seen it several times in others' flow. Double quote is fine, since sometimes we want to dynamically remove other variable.

I just proof the manual sorting is possible, and has made the flow to sort my glovar. Not a very important flow, but a nice exercise.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: addMapEntry to index 0

Post by anuraag » 22 Dec 2017 23:37

Thanks for tips.

Post Reply