最近在看一些Android应用性能优化的文章时,发现提到了SparseArray替代HashMap可以优化app性能,就对SparseArray做了一番了解,并记录使用心得。
我们来看看SparseArray点击进去包含了那些方法
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package android.util; public class SparseArray<E> implements Cloneable { public SparseArray() { throw new RuntimeException("Stub!"); } public SparseArray(int initialCapacity) { throw new RuntimeException("Stub!"); } public SparseArray<E> clone() { throw new RuntimeException("Stub!"); } public E get(int key) { throw new RuntimeException("Stub!"); } public E get(int key, E valueIfKeyNotFound) { throw new RuntimeException("Stub!"); } public void delete(int key) { throw new RuntimeException("Stub!"); } public void remove(int key) { throw new RuntimeException("Stub!"); } public void removeAt(int index) { throw new RuntimeException("Stub!"); } public void removeAtRange(int index, int size) { throw new RuntimeException("Stub!"); } public void put(int key, E value) { throw new RuntimeException("Stub!"); } public int size() { throw new RuntimeException("Stub!"); } public int keyAt(int index) { throw new RuntimeException("Stub!"); } public E valueAt(int index) { throw new RuntimeException("Stub!"); } public void setValueAt(int index, E value) { throw new RuntimeException("Stub!"); } public int indexOfKey(int key) { throw new RuntimeException("Stub!"); } public int indexOfValue(E value) { throw new RuntimeException("Stub!"); } public void clear() { throw new RuntimeException("Stub!"); } public void append(int key, E value) { throw new RuntimeException("Stub!"); } public String toString() { throw new RuntimeException("Stub!"); } }
增加
public void put(int key, E value) { throw new RuntimeException("Stub!"); } public void append(int key, E value) { throw new RuntimeException("Stub!"); }
通过键值对方式存储。
删除
public void delete(int key) { throw new RuntimeException("Stub!"); } public void remove(int key) { throw new RuntimeException("Stub!"); } public void removeAt(int index) { throw new RuntimeException("Stub!"); } public void removeAtRange(int index, int size) { throw new RuntimeException("Stub!"); } public void clear() { throw new RuntimeException("Stub!"); }
delete,remove根据key来删除,removeAt根据下标删除,removeAtRange根据下标范围删除。
改变
public void setValueAt(int index, E value) { throw new RuntimeException("Stub!"); } public void put(int key, E value) { throw new RuntimeException("Stub!"); }
setValueAt根据下标来重新赋值,put通过key来重新赋值,有就重新赋值,没有就添加。
查找
public E get(int key) { throw new RuntimeException("Stub!"); } //设置没有查找到的返回信息 public E get(int key, E valueIfKeyNotFound) { throw new RuntimeException("Stub!"); } public int keyAt(int index) { throw new RuntimeException("Stub!"); } public E valueAt(int index) { throw new RuntimeException("Stub!"); } public int indexOfKey(int key) { throw new RuntimeException("Stub!"); } public int indexOfValue(E value) { throw new RuntimeException("Stub!"); }
get根据key来查找。keyAt根据下标查找key值,valueAt通过下标查找value,indexOfKey通过key查询下标,indexOfValue通过value查找下标。
构造方法
public SparseArray(int initialCapacity) { throw new RuntimeException("Stub!"); }
可以初始化长度,SparseArray array=new SparseArray<>(5);
SparseIntArray intArray=new SparseIntArray(); SparseBooleanArray booleanArray=new SparseBooleanArray(); SparseLongArray longArray=new SparseLongArray();
来取代相应的HashMap
来自: http://blog.csdn.net/qq_16131393/article/details/51821210