Collection refers to a task when software development is concerned. HashMap is first data structure to emerge in mind when we look to store data in key-value pairs. On behalf of its flexibility, it is the most popular data structure choice. In order to boost the performance of Android App, there are two important set of collection ArrayMap and SparseArray are built exclusively for mobile. Thereby, substantially more memory efficient in comparison to a HashMap collection.
ArrayMap Working:
There are 2 smaller arrays inside an ArrayMap. Hash-Array (first array) holds specific hash keys in a sorted manner. The keys and values relating to the objects as per the first array are contained in the second array. The second array is also known as Key Value Array. A binary search is done on the Hash-Array upon fetching an item to find a matching hash the index after which key-value pair from the second array is returned directly. In case, when the key is inside the second array doesn’t match then the second array is used to avoid the collision.
Image Credits: android.jlelse.eu
Below code is used for the creation of ArrayMap and fetch keys and values:
ArrayMap<String, String> arrayMap = new ArrayMap<>();
arrayMap.put(“Key1”, “Value1”);
arrayMap.put(“Key2”, “Value2”);
arrayMap.put(“Key3”, “Value3”);
for (int i = 0; i < arrayMap.size(); i++) {
String key = arrayMap.keyAt(i);
String value = arrayMap.valueAt(i);
}
SparseArray Working:
The major difference between ArrayMap and SpareArray is that latter is always primitive types. Although their strategy of operation is quite similar. Sparse arrays are used to replace hash maps when the key is of the primitive type. Auto-boxing issue can be easily resolved with SparseArray which is not achievable by ArrayMap. Approach to resolve auto-boxing impacts the memory consumption.
Below code is used for the creation of ArrayMap and fetch keys and values:
SparseArray sparseArray = new SparseArray();
sparseArray.put(1, “Value1”);
SparseLongArray sparseLongArray = new SparseLongArray();
sparseLongArray.put(1, 1L);
SparseBooleanArray sparseBooleanArray = new SparseBooleanArray();
sparseBooleanArray.put(1, true);
SparseIntArray sparseIntArray = new SparseIntArray();
sparseIntArray.put(1, 2);
The Comparison
A lag can emerge in the Android application and limit the application performance which is due to continuous memory allocation and de-allocation. Both ArrayMap & SparseArray avoid memory problem avoid the major issue of memory problem with 2 small arrays than single big one.
How SparseArray scores over HashMap:
- Use of primitives makes it more memory efficient.
- No auto-boxing
- Allocation free
The Drawbacks:
- It is considerably slow for larger collections.
- Its availability is limited to And
Looking forward to responding to your queries and comments regarding ArrayMap and SparseArray in Android. If any information you want to get included I will update this article with it. Generally, when inserts and deletes are not that frequent such that quantity of items remains less than 1000 then SparseArray/ArrayMap are best possible replacement classes.
About Singsys Pte. Ltd. Singsys is a solution provider that offer user friendly solution on cutting edge technologies to engage customers and boost your brand online results from a set of certified developers, designers who prefer optimized utilization of the available resources to align client’s idea with their skillset to reflect it into a Mobile application, Web application or an E-commerce solution
You may be interested in following:
Related Posts...
Android
Jun 27th, 2024
On National Doctor’s Day, let’s appreciate the advancements in healthcare, especially the rise of telemedicine. This innovation has revolutionized medical care, making it more convenient and effective. India’s eSanjeevani platform stands out, providing equitable healthcare to all, especially in rural areas. Launched by the Ministry of Health and Family Welfare, eSanjeevani facilitates remote consultations and enhances the quality of care. As telemedicine continues to evolve, driven by technological advancements, it promises to play a crucial role in the future of healthcare, ensuring accessibility and efficiency for everyone.
Read more
Jun 25th, 2024
Over the years, educational technology has undergone a significant transformation. From traditional classroom settings to the advent of online learning platforms, the landscape of education continues to evolve. One of […]
Read more
May 7th, 2024
Have you ever considered how convenient it would be to order groceries online and deliver them to your doorstep? This has become more popular, especially after the COVID-19 pandemic. People […]
Read more