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方法還可以修改鍵值對,注意:如果鍵不存在,就會變為添加新鍵值對

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM