Android应用性能优化之使用SparseArray
2016-07-04 15:27
267 查看
最近在看一些Android应用性能优化的文章时,发现提到了SparseArray替代HashMap可以优化app性能,就对SparseArray做了一番了解,并记录使用心得。
一,我们来看看SparseArray点击进去包含了那些方法:
二,增加
通过键值对方式存储。
三,删除
delete,remove根据key来删除,removeAt根据下标删除,removeAtRange根据下标范围删除。
四,改变
setValueAt根据下标来重新赋值,put通过key来重新赋值,有就重新赋值,没有就添加。
五,查找
get根据key来查找。keyAt根据下标查找key值,valueAt通过下标查找value,indexOfKey通过key查询下标,indexOfValue通过value查找下标。
构造方法:
可以初始化长度,SparseArray array=new SparseArray<>(5);
来取代相应的HashMap
一,我们来看看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
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories