一、介绍
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