<title>Android app performance optimization using Sparsearray instead of HashMap</title> Date: December 31, 2015 09:53:03
Sparsearray refers to a sparse array (Sparse array), where the so-called sparse array is where most of the content values in the array are not used (or both 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 the size of the container, the default size is 10.
Take a look at its "additions and deletions" to the data.
It has two ways to add a key-value pair:
Public void put (int key, E value) {}Public void Append (int key, E value) {}
There are four ways to perform a delete operation:
Public void Delete (int key) {}Public void Remove (int key) {}//delete directly called (int key)Public void removeAt (int index) {}Public void Clear () {}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. Therefore, there are two ways to modify the data actually:
Public void put (int key, E value)Public void setvalueat (int index, E value)Finally, let's look at how to find data. There are two ways to query a value:
Public E get (int key)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 value returned. Get (int key) when not found, Default returns NULL.
to view the keys for the first few positions: 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:
Public E valueat (int index)
to see where the value is, no, return-1:
public int Indexofvalue (E value)
Summary:
Sparsearray is a class specifically written for such hashmap in Android, designed to improve efficiency, the core of which is the binary lookup function (binarysearch). In Android, when we need to defineHashMap <integer, e> HashMap = new HashMap <integer, e> ();, we can use the following methods to achieve better performance.sparsearray <E> sparsearray = new Sparsearray <E> ();
From for notes (Wiz)
Android app performance optimization using Sparsearray instead of HashMap