关于原理及相应方法我借用两篇博客如下:
第一篇:
最近编程时,发现一个针对HashMap<Integer, E>的一个提示:
翻译过来就是:用SparseArray<E>来代替会有更好性能。
那我们就来看看源码中SparseArray到底做了哪些事情:
一、构造
从构造方法我们可以看出,它和一般的List一样,可以预先设置容器大小,默认的大小是10:
- public SparseArray() {
- this(10);
- }
- public SparseArray(int initialCapacity) {
- ......
- }
二、增
它有两个方法可以添加键值对:
- public void put(int key, E value)
- public void append(int key, E value)
在存储数据的时候,是采用了二分法方式,以下是它采用二分法的源码:
- 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 ^ 0xFFFFFFFF;
- if (a[high] == key) {
- return high;
- }
- return high ^ 0xFFFFFFFF;
- }
所以, 它存储的数值都是按键值从小到大的顺序排列好的。
三、查
它有两个方法可以取值:
- public E get(int key)
- public E get(int key, E valueIfKeyNotFound)
查看第几个位置的键:
- public int keyAt(int index)
- public E valueAt(int index)
- public int indexOfKey(int key)
- public int indexOfValue(E value)
四、删
它有四个方法:
- public void delete(int key)
- public void remove(int key)
但其实,delete和remove的效果是一样的,remove方法中调用了delete方法,remove源码:
- public void remove(int key) {
- delete(key);
- }
- public void removeAt(int index)
- public void clear()
最后一个就是清除全部
五、改
- public void setValueAt(int index, E value)
- public void put(int key, E value)
put方法还可以修改键值对, 注意:如果键不存在,就会变为添加新键值对
六、其他:
SparseArray实现了Cloneable接口,还可以调用clone方法。
第二篇:
一、介绍
SparseArray是 Android框架独有的类,在标准的JDK中不存在这个类。它要比 HashMap 节省内存,某些情况下比HashMap性能更好,按照官方问答的解释,主要是因为SparseArray不需要对key和value进行auto- boxing(将原始类型封装为对象类型,比如把int类型封装成Integer类型),结构比HashMap简单(SparseArray内部主要使用 两个一维数组来保存数据,一个用来存key,一个用来存value)不需要额外的额外的数据结构(主要是针对HashMap中的HashMapEntry 而言的)。
单纯从字面上来理解,SparseArray指的是稀疏数组(Sparse array),所谓稀疏数组就是数组中大部分的内容值都未被使用(或都为零),在数组中仅有少部分的空间使用。因此造成内存空间的浪费,为了节省内存空间,并且不影响数组中原有的内容值,我们可以采用一种压缩的方式来表示稀疏数组的内容。
文中关于稀疏数组(Sparse array)的定义说明参照至:
http://hi.baidu.com/piaopiao_0423/item/d8cc2b99729f8380581461d1
二、使用
SparseArray<String> sparseArray=new SparseArray<String>();
//增加 两种方式
sparseArray.append(0, "This is 0");
sparseArray.append(1, "This is 1");
sparseArray.append(2, "This is 2");
sparseArray.put(3, "This is 3");
sparseArray.put(4, "This is 4");
//修改 两种方式
sparseArray.put(0, "This is new 0");//在put数据之前,会先查找要put的数据是否已经存在,如果存在就是修改,不存在就添加。
sparseArray.put(1, "This is new 1");
sparseArray.setValueAt(2, "This is new 2");
sparseArray.setValueAt(3, "This is new 3");
//删除 四种方式
sparseArray.delete(0);
sparseArray.remove(0); //直接调用的delete(int key)
sparseArray.removeAt(0);
sparseArray.clear();
//查找数据
sparseArray.get(0);//当找不到的时候,默认返回null
sparseArray.get(0, "找不到的默认值");
//遍历, 注意:
//在执行删除后sparseArray的size()是变化的,因此在遍历前,先求值。
for (int i = 0,j=sparseArray.size(); i < j; i++) {
System.out.println("遍历得到位置"+i+"的值为:"+sparseArray.get(i));
}
//查找某个位置的键 ,注意:查看键所在位置,由于是采用二分法查找键的位置,所以找不到时返回小于0的数值,而不是返回-1。返回的负值是表示它在找不到时所在的位置。
int key =sparseArray.keyAt(1);
System.out.println("查找位置1处的键 key="+key);
//查找某个位置的值
String value=(String) sparseArray.valueAt(1);
System.out.println("查找位置1处的值 value="+value);
//查看值所在位置,没有的话返回-1:
int index =sparseArray.indexOfValue("aa");
三. 其它
1. 在正序插入数据时候,SparseArray比HashMap要快一些;HashMap不管是倒序还是正序开销几乎是一样的;但是SparseArray的倒序插入要比正序插入要慢10倍以上。(因为SparseArray在检索数据的时候使用的是二分查找,所以每次插入新数据的时候SparseArray都需要重新排序,所以代码4中,逆序是最差情况。)2. 相应的也有SparseBooleanArray,用来取代HashMap<Integer, Boolean>,SparseIntArray用来取代HashMap<Integer, Integer>,用法基本类似。
四、可序列化的扩展
地址:http://stackoverflow.com/questions/14899718/how-to-store-sparsearray-in-bundle
public class SerializableSparseArray<E> extends SparseArray<E> implements Serializable{
private static final long serialVersionUID = 824056059663678000L;
public SerializableSparseArray(int capacity){
super(capacity);
}
public SerializableSparseArray(){
super();
}
/**
* This method is private but it is called using reflection by java
* serialization mechanism. It overwrites the default object serialization.
*
* <br/><br/><b>IMPORTANT</b>
* The access modifier for this method MUST be set to <b>private</b> otherwise {@link java.io.StreamCorruptedException}
* will be thrown.
*
* @param oos
* the stream the data is stored into
* @throws IOException
* an exception that might occur during data storing
*/
private void writeObject(ObjectOutputStream oos) throws IOException {
Object[] data = new Object[size()];
for (int i=data.length-1;i>=0;i--){
Object[] pair = {keyAt(i),valueAt(i)};
data[i] = pair;
}
oos.writeObject(data);
}
/**
* This method is private but it is called using reflection by java
* serialization mechanism. It overwrites the default object serialization.
*
* <br/><br/><b>IMPORTANT</b>
* The access modifier for this method MUST be set to <b>private</b> otherwise {@link java.io.StreamCorruptedException}
* will be thrown.
*
* @param oos
* the stream the data is read from
* @throws IOException
* an exception that might occur during data reading
* @throws ClassNotFoundException
* this exception will be raised when a class is read that is
* not known to the current ClassLoader
*/
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
Object[] data = (Object[]) ois.readObject();
for (int i=data.length-1;i>=0;i--){
Object[] pair = (Object[]) data[i];
this.append((Integer)pair[0],(E)pair[1]);
}
return;
}
}
参考:
http://liuzhichao.com/p/832.html
http://www.open-open.com/lib/view/open1402906434918.html
内部实现:
SparseArray内部主要是通过两个数组实现的,一个数组用来保存key,一个数组用来保存value。
稀疏数组的使用,对于索引是整数的情景,有时能带来一些效率的提升。
1. 减少了hashCode时间消耗
2. 减小了所使用的内存大小。
但在所管理的对象数量很大时,效率却反而有可能更低:
在插入的时候,有可能导致大段数组的复制;
在删除之后,也有可能导致数组的大段元素被按个移动(不是复制数组,而是一个一个单独移动);