When i shoud use an arrayMap instead of an arraylist? I read the documentation but it seems that they are interchangeable,so can someone provide me an example of when is better to use one and when the other?
Asked
Active
Viewed 109 times
1 Answers
1
From the official docs:
ArrayMap is a generic key->value mapping data structure that is designed to be more memory efficient than a traditional HashMap, this implementation is a version of the platform's android.util.ArrayMap that can be used on older versions of the platform. It keeps its mappings in an array data structure -- an integer array of hash codes for each item, and an Object array of the key/value pairs.
So in short, ArrayMap is a Key-Value data structure
. On the other ArrayList is just a List of elements
(which behind the curtain uses an Array to manage those elements)

Rene Ferrari
- 4,096
- 3
- 22
- 28
-
Thanks,can you give me a quick example? – Matteo Crosilla Feb 08 '19 at 09:25
-
A List in general should always be used if you have elements in an undefined quantity. Lets say you fetch users from a database: Since you don't know how many users you will get, so using an Array is not a viable option. That's a usecase for a List. There is a difference between ArrayList and LinkedList though and when to use which is dependent on the usecase ([check this out for more information](https://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist-in-java)) – Rene Ferrari Feb 08 '19 at 09:28
-
ArrayMap is - in its core - similar to SharedPreferences in the sense that the structure of both is the same -> a key value store. With an ArrayMap you could for example (though not the most useful usecase ;D) map your users fetched from the database by using their Id as key and the User object as value. – Rene Ferrari Feb 08 '19 at 09:36