SparseArray for Android programming & lt; E & gt; details

Source: Internet
Author: User

During recent programming, we found a prompt for HashMap <Integer, E>:


The translation is: Use SparseArray <E> to replace it with better performance.
Let's take a look at what SparseArray has done in the source code:

I. Structure
We can see from the constructor that it is the same as the General List, you can set the container size in advance, the default size is 10:
[Java]
Public SparseArray (){
This (10 );
}
 
 
Public SparseArray (int initialCapacity ){
......
}


Ii. Add
There are two ways to add a key-Value Pair:
[Java]
Public void put (int key, E value)
Public void append (int key, E value)

 

Data is stored in the binary method. The following is the source code of the binary method:
[Java]
Private static int binarySearch (int [] a, int start, int len, int key ){
Int high = start + len;
Int low = start-1;
 
 
While (high-low> 1 ){
Int guess = (high + low)/2;
 
 
If (a [guess] <key ){
Low = guess;
Continue;
}
High = guess;
}
 
 
If (high = start + len)
Return start + len ^ 0 xFFFFFFFF;
If (a [high] = key ){
Return high;
}
Return high ^ 0 xFFFFFFFF;
}

 

Therefore, the stored values are sorted in ascending order of key values.


Iii. Query
It has two methods:
[Java]
Public E get (int key)
Public E get (int key, E valueIfKeyNotFound)
The last one can be seen from the variable name of the passing parameter. The input value is the value returned when it cannot be found.


View the keys at the following locations:
[Java]
Public int keyAt (int index)
View the value at the following position:
[Java]
Public E valueAt (int index)
Check the key location. Because the binary method is used to find the key location, if not, a value smaller than 0 is returned instead of-1. Note that, the returned negative number indicates the location where it cannot be found. If you store five keys and the search key value is greater than five, the return value is-6:
[Java]
Public int indexOfKey (int key)
View the location of the value. If not,-1 is returned:
[Java]
Public int indexOfValue (E value)


Iv. Delete
It has four methods:
[Java]
Public void delete (int key)
Public void remove (int key)

In fact, the effects of delete and remove are the same. The delete method is called in the remove method. The remove source code is as follows:

[Java]
Public void remove (int key ){
Delete (key );
}


[Java]
Public void removeAt (int index)
Public void clear ()

The last one is to clear all


V. Change
[Java]
Public void setValueAt (int index, E value)
Public void put (int key, E value)

The put method can also modify the key-value pair. Note: if the key does not exist, the new key-value pair will be added.


6. Others:
SparseArray implements the Cloneable interface and can also call the clone method.


Conclusion: We recommend that you use SparseArray <E> to replace HashMap <Integer, E> In the android system. Let's do what it says. I have analyzed some of the methods here and hope they will help you.

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.