概要:
SparseArray是android.util中提供的类,用于存储整数-Object映射。
相对于我们常用的HashMap,它的性能更好:
1,避免了自动装箱过程。(java中将基本数据类型自动转化成转载器数据类,拆箱:将装载器数据类型转化成
基本数据类型);
2,使用二分法对SparseArray的key检索。
分析:
public class SparseArray implements Cloneable {
private static final Object DELETED = new Object();//已删除值通用的标记对象
private boolean mGarbage = false;//是否需要gc的标志
private int[] mKeys;//key 数组
private Object[] mValues;//value 数组
private int mSize;// SparseArray的当前存储量。
/**
* Creates a new SparseArray containing no mappings.
*/
public SparseArray() {
this(10);
}
public SparseArray(int initialCapacity) {
if (initialCapacity == 0) {
mKeys = EmptyArray.INT;
mValues = EmptyArray.OBJECT;
} else {
mValues = ArrayUtils.newUnpaddedObjectArray(initialCapacity);
mKeys = new int[mValues.length];
}
mSize = 0;
}
构造函数可以看出,初始化的容量大小为10。
/**
* Adds a mapping from the specified key to the specified value,
* replacing the previous mapping from the specified key if there
* was one.
*/
public void put(int key, E value) {
int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
if (i >= 0) {
mValues[i] = value;
} else {
i = ~i;
if (i < mSize && mValues[i] == DELETED) {
mKeys[i] = key;
mValues[i] = value;
return;
}
if (mGarbage && mSize >= mKeys.length) {
gc();
// Search again because indices may have changed.
i = ~ContainerHelpers.binarySearch(mKeys, mSize, key);
}
mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
mValues = GrowingArrayUtils.insert(mValues, mSize, i, value);
mSize++;
}
}
其中ContainerHelpers.binarySearch方法如下:
static int binarySearch(int[] array, int size, int value) {
int lo = 0;
int hi = size - 1;
while (lo <= hi) {
final int mid = (lo + hi) >>> 1;
final int midVal = array[mid];
if (midVal < value) {
lo = mid + 1;
} else if (midVal > value) {
hi = mid - 1;
} else {
return mid; // value found
}
}
return ~lo; // value not present
}
1,上面是典型的二分法遍历,找到了就放回对应的下标,否则返回对应位置的值取负。
2,上面put方法,当找到了对应的key,直接赋值。没有匹配到相应的key值,且返回的index,上值被标记DELETED重新赋值。
如果大小超标,先gc,然后数组扩容翻倍。
public void remove(int key) {
delete(key);
}
/**
* Removes the mapping from the specified key, if there was any.
*/
public void delete(int key) {
int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
if (i >= 0) {
if (mValues[i] != DELETED) {
mValues[i] = DELETED;
mGarbage = true;
}
}
}
删除(remove,delete)方法,二分法查找key,对应的value标记成已删除标示,mGarbage待gc标记为true。
这里没有立即的移除数组元素。
public E get(int key) {
return get(key, null);
}
public E get(int key, E valueIfKeyNotFound) {
int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
if (i < 0 || mValues[i] == DELETED) {
return valueIfKeyNotFound;
} else {
return (E) mValues[i];
}
}
get方法二分法查找key,找到并且value没有标记DElEED返回对应值,否则返回null
/**
* Returns the number of key-value mappings that this SparseArray
* currently stores.
*/
public int size() {
if (mGarbage) {
gc();
}
return mSize;
}
private void gc() {
// Log.e("SparseArray", "gc start with " + mSize);
int n = mSize;
int o = 0;
int[] keys = mKeys;
Object[] values = mValues;
for (int i = 0; i < n; i++) {
Object val = values[i];
if (val != DELETED) {
if (i != o) {
keys[o] = keys[i];
values[o] = val;
values[i] = null;
}
o++;
}
}
mGarbage = false;
mSize = o;
// Log.e("SparseArray", "gc end with " + mSize);
}
size()方法,先gc,然后返回mSize,大家有没有注意到delete的时候,mSize并有--。所以mSize
已经不对了,但是gc过后就是正确的。因为它重新遍历,去掉了无用数据。