Android应用性能优化之使用SparseArray

ig1032 8年前
   <p>最近在看一些Android应用性能优化的文章时,发现提到了SparseArray替代HashMap可以优化app性能,就对SparseArray做了一番了解,并记录使用心得。</p>    <h2><strong>我们来看看SparseArray点击进去包含了那些方法</strong></h2>    <pre>  <code class="language-java">//  // 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!");      }  }  </code></pre>    <h2><strong>增加</strong></h2>    <pre>  <code class="language-java">  public void put(int key, E value) {          throw new RuntimeException("Stub!");      }    public void append(int key, E value) {          throw new RuntimeException("Stub!");      }</code></pre>    <p>通过键值对方式存储。</p>    <h2><strong>删除</strong></h2>    <pre>  <code class="language-java"> 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!");      }</code></pre>    <p>delete,remove根据key来删除,removeAt根据下标删除,removeAtRange根据下标范围删除。</p>    <h2><strong>改变</strong></h2>    <pre>  <code class="language-java"> public void setValueAt(int index, E value) {          throw new RuntimeException("Stub!");      }   public void put(int key, E value) {          throw new RuntimeException("Stub!");      }</code></pre>    <p>setValueAt根据下标来重新赋值,put通过key来重新赋值,有就重新赋值,没有就添加。</p>    <h2><strong>查找</strong></h2>    <pre>  <code class="language-java"> 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!");      }  </code></pre>    <p>get根据key来查找。keyAt根据下标查找key值,valueAt通过下标查找value,indexOfKey通过key查询下标,indexOfValue通过value查找下标。</p>    <h2><strong>构造方法</strong></h2>    <pre>  <code class="language-java">    public SparseArray(int initialCapacity) {          throw new RuntimeException("Stub!");      }</code></pre>    <p>可以初始化长度,SparseArray array=new SparseArray<>(5);</p>    <pre>  <code class="language-java">SparseIntArray intArray=new SparseIntArray();  SparseBooleanArray booleanArray=new SparseBooleanArray();  SparseLongArray longArray=new SparseLongArray();</code></pre>    <p>来取代相应的HashMap</p>    <p> </p>    <p>来自: <a href="/misc/goto?guid=4959675051591855644" rel="nofollow">http://blog.csdn.net/qq_16131393/article/details/51821210</a></p>    <p> </p>