SparseArray 详解

时间:2019-11-11
本文章向大家介绍SparseArray 详解,主要包括SparseArray 详解使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
最近编程时,发现一个针对HashMap<Integer, E>的一个提示:

Use new SparseArray<Bitmap>(...) instead for better performance

翻译过来就是:用 SparseArray<E> 来代替(HashMap)会有更好性能。
那我们就来看看源码中 SparseArray 到底做了哪些事情:

-------------------------------------------------------------------------------
一、构造

从构造方法我们可以看出,他和一般的 List 一样,可以预先设置容器大小,默认的大小是 10/**
     * Creates a new SparseArray containing no mappings.
     */
    public SparseArray() {

        this(10);
    }

-------------------------------------------------------------------------------
二、增
他有两个方法可以添加键值对:
public void put(int key, E value)
public void append(int key, E value)

在存储数据的时候,是采用了二分法方式,以下是他采用二分法的源码:

    private static int binarySearch(int[] a, int start, int len, int key) {

        int high = start + len;
        int low = start - 1;

        while (high - low > 1) {
            int guess = (high + low) / 2;

            if (a[guess] < key) {
                low = guess;
                continue;
            }
            high = guess;
        }

        if (high == start + len)
            return start + len ^ 0xFFFFFFFF;

        if (a[high] == key) {

            return high;
        }
        return high ^ 0xFFFFFFFF;
    }

所以,他存储的数值都是按键值从小到大的顺序排列好的。

-------------------------------------------------------------------------------
三、查

他有两个方法可以取值:
public E get(int key)
public E get(int key, E valueIfKeyNotFound)

最后一个从传参的变量名就能看出,传入的是找不到的时候返回的值

1.查看第几个位置的键:

public int keyAt(int index)

2.查看第几个位置的值:

public E valueAt(int index)

3.查看键所在位置,由于采用二分法查找键的位置,所以没有的话返回小于 0 的数值,而不是返回 -1,这点要注意,返回的负数其实是表示他在哪个位置就找不到了,如果你存了 5 个,查找的键大于 5 个值的话,返回就是 -6public int indexOfKey(int key)

4.查看值所在位置,没有的话返回 -1public int indexOfValue(E value)

5.遍历方法:

            SparseArray<MyData> tArray = new SparseArray<MyData>();

            // 添加数据
            for (int i = 0; i < 10; i++) {

                tArray.put(i, new MyData());
            }

            // 遍历方法 1
            for (int i = 0; i < tArray.size(); i++) {
                
              //如果你不关心键,然后 valuesAt(int) 同时通过稀疏数组迭代直接访问这些值可以到
                MyData tMyData=tArray.valueAt(i); 
            }
            
            // 遍历方法 2
            for (int i = 0; i < tArray.size(); i++) {
                
                int tKey = tArray.keyAt(i); // 先查出 0 —— 数组长度,下标的 Key 再按 Key 获取值
                MyData tMyData = tArray.get(tKey);
            }

-------------------------------------------------------------------------------
四、删

他有四个方法:

public void delete(int key)
public void remove(int key)

但其实,delete 和 remove 的效果是一样的,remove 方法中调用了 delete 方法,remove 源码:

    public void remove(int key) {

        delete(key);
    }

So:还是用 delete 方法吧

public void removeAt(int index) // 移除指定索引处的映射
public void clear() // 清除全部

-------------------------------------------------------------------------------
五、改

public void setValueAt(int index, E value)
public void put(int key, E value)

put方法还可以修改键值对,注意:如果键不存在,就会变为添加新键值对

原文地址:https://www.cnblogs.com/zx-blog/p/11835950.html