Android應用性能優化之使用SparseArray

ig1032 8年前發布 | 14K 次閱讀 Android 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

 

來自: http://blog.csdn.net/qq_16131393/article/details/51821210

 

 本文由用戶 ig1032 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!