zoukankan      html  css  js  c++  java
  • android小知识之SparseArray(HaspMap替换)

    最近编程时,发现一个针对HashMap<Integer, E>的一个提示:


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

    一、构造
    从构造方法我们可以看出,它和一般的List一样,可以预先设置容器大小,默认的大小是10:

    [java] view plaincopy
     
    1. public SparseArray() {  
    2.     this(10);  
    3. }  
    4.   
    5.   
    6. public SparseArray(int initialCapacity) {  
    7.     ......  
    8. }  



    二、增
    它有两个方法可以添加键值对:

    [java] view plaincopy
     
    1. public void put(int key, E value)  
    2. public void append(int key, E value)   




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

    [java] view plaincopy
     
    1. private static int binarySearch(int[] a, int start, int len, int key) {  
    2.     int high = start + len;  
    3.     int low = start - 1;  
    4.   
    5.   
    6.     while (high - low > 1) {  
    7.         int guess = (high + low) / 2;  
    8.   
    9.   
    10.         if (a[guess] < key) {  
    11.             low = guess;  
    12.             continue;  
    13.         }  
    14.         high = guess;  
    15.     }  
    16.   
    17.   
    18.     if (high == start + len)  
    19.         return start + len ^ 0xFFFFFFFF;  
    20.     if (a[high] == key) {  
    21.         return high;  
    22.     }  
    23.     return high ^ 0xFFFFFFFF;  
    24. }  




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


    三、查
    它有两个方法可以取值:

    [java] view plaincopy
     
    1. public E get(int key)  
    2. public E get(int key, E valueIfKeyNotFound)  

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


    查看第几个位置的键:

    [java] view plaincopy
     
    1. public int keyAt(int index)  

    查看第几个位置的值:

    [java] view plaincopy
     
    1. public E valueAt(int index)  

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

    [java] view plaincopy
     
    1. public int indexOfKey(int key)  

    查看值所在位置,没有的话返回-1:

    [java] view plaincopy
     
    1. public int indexOfValue(E value)  



    四、删
    它有四个方法:

    [java] view plaincopy
     
    1. public void delete(int key)  
    2. public void remove(int key)  


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

    [java] view plaincopy
     
    1. public void remove(int key) {  
    2.         delete(key);  
    3.     }  

     

    [java] view plaincopy
     
    1. public void removeAt(int index)  
    2. public void clear()  


    最后一个就是清除全部


    五、改

    [java] view plaincopy
     
    1. public void setValueAt(int index, E value)  
    2. public void put(int key, E value)  


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


    六、其他:
    SparseArray实现了Cloneable接口,还可以调用clone方法。


    小结:既然android系统建议我们用SparseArray<E>来代替HashMap<Integer, E>,那我们还是按它说的做吧。里面的一些方法,我在这里也已经剖析清楚了,希望能对你们有所帮助。

  • 相关阅读:
    算法竞赛入门经典习题2-3 韩信点兵
    ios入门之c语言篇——基本函数——5——素数判断
    ios入门之c语言篇——基本函数——4——数值交换函数
    144. Binary Tree Preorder Traversal
    143. Reorder List
    142. Linked List Cycle II
    139. Word Break
    138. Copy List with Random Pointer
    137. Single Number II
    135. Candy
  • 原文地址:https://www.cnblogs.com/ws5861/p/3482239.html
Copyright © 2011-2022 走看看