Android中关于SparseArray使用
通常情况下,当我们用HashMap存储数据时,Android studio会建议我们使用SparseArray,最近公司项目使用到了,所以就来探究一下
构造器
1.无参构造器:SparseArray(),源码如下:
/**
* Creates a new SparseArray containing no mappings.
*/
public SparseArray() {
this(10);
}
2.带参构造器:SparseArray(int initialCapacity),源码如下:
/**
* Creates a new SparseArray containing no mappings that will not
* require any additional memory allocation to store the specified
* number of mappings. If you supply an initial capacity of 0, the
* sparse array will be initialized with a light-weight representation
* not requiring any additional array allocations.
*/
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
下面着重看看它的几个方法
一.添加键值对
1.public void put(int key, E value) ,源码如下:
/**
* 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++;
}
}
2.public void append(int key, E value) ,源码如下:
/**
* Puts a key/value pair into the array, optimizing for the case where
* the key is greater than all existing keys in the array.
*/
public void append(int key, E value) {
if (mSize != 0 && key <= mKeys[mSize - 1]) {
put(key, value);
return;
}
if (mGarbage && mSize >= mKeys.length) {
gc();
}
mKeys = GrowingArrayUtils.append(mKeys, mSize, key);
mValues = GrowingArrayUtils.append(mValues, mSize, value);
mSize++;
}
可以看出,采用的是二分法存储,存储数据是按键的值从小到大的顺序排列的。
二.查
1.根据见键查询值
public E get(int key) //查不到时为null
public E get(int key, E valueIfKeyNotFound) //valueIfKeyNotFound 当查不到时的默认值
源码如下:
/**
* Gets the Object mapped from the specified key, or <code>null</code>
* if no such mapping has been made.
*/
public E get(int key) {
return get(key, null);
}
/**
* Gets the Object mapped from the specified key, or the specified Object
* if no such mapping has been made.
*/
@SuppressWarnings("unchecked")
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];
}
}
.
.
.
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
}
2.查看某个位置的键:
public int keyAt(int index)
3.查看某个位置的值:
public E valueAt(int index)
4.根据Key查询键所在的位置,没有则返回负数
public int indexOfKey(int key)
5.根据Value查询值所在的位置,没有则返回-1
public int indexOfValue(E value)
三.删除
public void delete(int key)
public void remove(int key)
public void removeAt(int index)
四.修改
public void setValueAt(int index, E value) //比较常用
public void put(int key, E value)