Android应用性能优化之使用SparseArray

最近在看一些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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值