Android特有的數(shù)據(jù)結(jié)構(gòu)分析

android為了減少內(nèi)存的使用和裝箱拆箱損耗的性能,提供一些特有的數(shù)據(jù)接口,在 android.util包下面,都是使用數(shù)據(jù)進(jìn)行保存,適當(dāng)?shù)氖褂眠@些對象可以優(yōu)化我們的應(yīng)用

  • ArrayMap
  • ArraySet
  • SparseArray
  • SparseIntArray
  • SparseBooleanArray
  • SparseLongArray

ArrayMap可代替Map<Integer,Objects>,且key唯一

//保存hash
int[] mHashes;
//保存key和value
Object[] mArray;
 public V put(K key, V value) {
        final int hash;
        int index;
        //先計(jì)算index取反的值
        if (key == null) {
            hash = 0;
            index = indexOfNull();
        } else {
            hash = mIdentityHashCode ? System.identityHashCode(key) : key.hashCode();
            index = indexOf(key, hash);
        }
        if (index >= 0) {
            index = (index<<1) + 1;
            final V old = (V)mArray[index];
            mArray[index] = value;
            return old;
        }

        index = ~index;
        if (mSize >= mHashes.length) {
            final int n = mSize >= (BASE_SIZE*2) ? (mSize+(mSize>>1))
                    : (mSize >= BASE_SIZE ? (BASE_SIZE*2) : BASE_SIZE);

            if (DEBUG) Log.d(TAG, "put: grow from " + mHashes.length + " to " + n);

            final int[] ohashes = mHashes;
            final Object[] oarray = mArray;
            allocArrays(n);

            if (mHashes.length > 0) {
                if (DEBUG) Log.d(TAG, "put: copy 0-" + mSize + " to 0");
                System.arraycopy(ohashes, 0, mHashes, 0, ohashes.length);
                System.arraycopy(oarray, 0, mArray, 0, oarray.length);
            }

            freeArrays(ohashes, oarray, mSize);
        }

        //擴(kuò)展數(shù)組
        if (index < mSize) {
            if (DEBUG) Log.d(TAG, "put: move " + index + "-" + (mSize-index)
                    + " to " + (index+1));
            System.arraycopy(mHashes, index, mHashes, index + 1, mSize - index);
            System.arraycopy(mArray, index << 1, mArray, (index + 1) << 1, (mSize - index) << 1);
        }

        mHashes[index] = hash;
        //key value連續(xù)保存
        mArray[index<<1] = key;
        mArray[(index<<1)+1] = value;
        mSize++;
        return null;
    }

ArraySet
跟ArrayMap數(shù)據(jù)結(jié)構(gòu)一樣,只保存value,value唯一

int[] mHashes;
    Object[] mArray;
 public boolean add(E value) {
        final int hash;
        int index;
        if (value == null) {
            hash = 0;
            index = indexOfNull();
        } else {
            hash = mIdentityHashCode ? System.identityHashCode(value) : value.hashCode();
            index = indexOf(value, hash);
        }
        if (index >= 0) {
            return false;
        }

        index = ~index;
        if (mSize >= mHashes.length) {
            final int n = mSize >= (BASE_SIZE*2) ? (mSize+(mSize>>1))
                    : (mSize >= BASE_SIZE ? (BASE_SIZE*2) : BASE_SIZE);

            if (DEBUG) Log.d(TAG, "add: grow from " + mHashes.length + " to " + n);

            final int[] ohashes = mHashes;
            final Object[] oarray = mArray;
            allocArrays(n);

            if (mHashes.length > 0) {
                if (DEBUG) Log.d(TAG, "add: copy 0-" + mSize + " to 0");
                System.arraycopy(ohashes, 0, mHashes, 0, ohashes.length);
                System.arraycopy(oarray, 0, mArray, 0, oarray.length);
            }

            freeArrays(ohashes, oarray, mSize);
        }

        if (index < mSize) {
            if (DEBUG) Log.d(TAG, "add: move " + index + "-" + (mSize-index)
                    + " to " + (index+1));
            System.arraycopy(mHashes, index, mHashes, index + 1, mSize - index);
            System.arraycopy(mArray, index, mArray, index + 1, mSize - index);
        }

        mHashes[index] = hash;
        mArray[index] = value;
        mSize++;
        return true;
    }

SparseIntArray
key-value映射類型,使用基礎(chǔ)類型int,原來替換Map<>Map<Integer,Integer>,免去裝箱拆箱操作,優(yōu)化內(nèi)存和性能

private int[] mKeys;
private int[] mValues;
    public void put(int key, int value) {
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);

        if (i >= 0) {
            mValues[i] = value;
        } else {
            i = ~i;

            mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
            mValues = GrowingArrayUtils.insert(mValues, mSize, i, value);
            mSize++;
        }
    }
    
    //GrowingArrayUtils.java
     public static <T> T[] insert(T[] array, int currentSize, int index, T element) {
        assert currentSize <= array.length;

        //數(shù)組長度足夠,插入數(shù)據(jù)
        if (currentSize + 1 <= array.length) {
            System.arraycopy(array, index, array, index + 1, currentSize - index);
            array[index] = element;
            return array;
        }

        //數(shù)字長度不足,一般擴(kuò)展2倍空間
        @SuppressWarnings("unchecked")
        T[] newArray = ArrayUtils.newUnpaddedArray((Class<T>)array.getClass().getComponentType(),
                growSize(currentSize));
        //復(fù)制index前面部分?jǐn)?shù)據(jù)
        System.arraycopy(array, 0, newArray, 0, index);
        newArray[index] = element;
         //復(fù)制index后面部分?jǐn)?shù)據(jù)
        System.arraycopy(array, index, newArray, index + 1, array.length - index);
        return newArray;
    }
    
        public static int growSize(int currentSize) {
        return currentSize <= 4 ? 8 : currentSize * 2;
    }

SparseLongArray 可代替Map<Integer,Long>

private int[] mKeys;
private long[] mValues;

SparseBooleanArray 可代替Map<Integer,Boolean>

 private int[] mKeys;
private boolean[] mValues;

SparseArray Map<Integer,Objects>

private int[] mKeys;
    private Object[] mValues;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容