Android app performance optimization using Sparsearray instead of HashMap

Source: Internet
Author: User

HashMap is a common collection class in Java, and I'm used to caching some of the processed results. When I was working on an Android project recently, defining such a variable in the code and instantiating it, Eclipse gave a performance warning.

It means replacing with sparsearray<e> to get better performance. To be honest, not familiar with Sparsearray, the first feeling should be a class provided by Android. Ctrl-click to enter the source of Sparsearray, sure enough, to determine the Android provides a tool class.

Purely literally, sparsearray refers to a sparse array (Sparse array), where the so-called sparse array is the majority of the content values in the array are not used (or all 0), and only a small portion of the space is used in the array. Therefore, the memory space wasted, in order to save memory space, and do not affect the contents of the array of content values, we can use a compressed way to represent the contents of the sparse array.

Suppose there is an array of 9*7, with the following contents:

In this array, there are 63 spaces, but only 5 elements are used, resulting in a waste of 58 element space. Let's use the sparse array to redefine the array:

Where the first part of the sparse array records the number of columns and rows of the original array and the number of elements used, the second part records the position and contents of the elements in the original array. After compression, the original need to declare an array of size 63, and after using compression, only need to declare an array of size 6*3, only 18 storage space.

Continue to read the Sparsearray source code, from the construction method we can see, it and the general list, can be pre-set container size, the default size is 10:

1 publicSparseArray() {
2     this(10);
3 }
4
5 publicSparseArray(intinitialCapacity) {
6     initialCapacity = ArrayUtils.idealIntArraySize(initialCapacity);
7
8     mKeys = newint[initialCapacity];
9     mValues = newObject[initialCapacity];
10     mSize = 0;
11 }

Take a look at its "additions and deletions" to the data.

It has two ways to add a key-value pair:

1 publicvoid put(intkey, E value) {}
2 publicvoid append(intkey, E value){}

There are four ways to perform a delete operation:

1 publicvoid delete(intkey) {}
2 publicvoid remove(intkey) {} //直接调用的delete(int key)
3 publicvoid removeAt(intindex){}
4 publicvoidclear(){}

Modifying the data initially thought that only setvalueat (int index, e value) could modify the data, but later found that the put (int key, E value) could also modify the data, and we looked at the source of the put (int key, E value), Before you put the data, you will find out whether the data you want to put already exists, and if so, add it if it exists.

1 publicvoid put(intkey, E value) {
2     inti = binarySearch(mKeys, 0, mSize, key);
3
4     if(i >= 0) {
5         mValues[i] = value;
6     else{
7         i = ~i;
8
9         if(i < mSize && mValues[i] == DELETED) {
10             mKeys[i] = key;
11             mValues[i] = value;
12             return;
13         }
14
15         if(mGarbage && mSize >= mKeys.length) {
16             gc();
17
18             // Search again because indices may have changed.
19             i = ~binarySearch(mKeys, 0, mSize, key);
20         }
21         …………

Therefore, there are two ways to modify the data actually:

1 public void put(int key, E value)
2 public void setValueAt(int index, E value)

Finally, let's look at how to find data. There are two ways to query a value:

1 public E get(int key)
2 public E get(int key, E valueIfKeyNotFound)

where get (int key) is also just called get (int key,e valueifkeynotfound), the last variable name from the argument can be seen, the incoming is not found when the return value. Get (int key) returns null by default when not found.

To view the keys in the first few places:

1 public int keyAt(int index)

One thing to note is that the view key is in the same position as the binary lookup key, so the value less than 0 is returned instead of 1 if it is not found. The negative value returned is the location where it was not found.

To see the values for the first few locations:

1 publicE valueAt(intindex)

To see where the value is, no, return-1:

1 publicintindexOfValue(E value)

Finally, it is found that the core is binary lookup function (binarysearch), algorithm design is very good.

1 privatestatic int binarySearch(int[] a, int start, int len, intkey) {
2     inthigh = start + len, low = start - 1, guess;
3
4     while(high - low > 1) {
5         guess = (high + low) / 2;
6
7         if(a[guess] < key)
8             low = guess;
9         else
10             high = guess;
11     }
12
13     if(high == start + len)
14         return~(start + len);
15     elseif(a[high] == key)
16         returnhigh;
17     else
18         return~high;
19 }

Corresponding also have Sparsebooleanarray, used to replace Hashmap<integer, Boolean>,sparseintarray used to replace Hashmap<integer, Integer> We are interested to be able to study.

Summary: Sparsearray is a class specifically written for <Interger,Object> such hashmap in Android, designed to improve efficiency, with the core of binary lookup function (binarysearch). In Android, when we need to define

1 HashMap<Integer, E> hashMap = newHashMap<Integer, E>();

, we can use the following methods to achieve better performance.

1 SparseArray<E> sparseArray = newSparseArray<E>();

Android app performance optimization using Sparsearray instead of HashMap

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.