日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【移动开发】SparseArray替代HashMap

發布時間:2025/3/15 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【移动开发】SparseArray替代HashMap 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SparseArray是android里為<Interger,Object>這樣的Hashmap而專門寫的class,目的是提高效率,其核心是折半查找函數(binarySearch)。

[java] view plaincopy
  • private?static?int?binarySearch(int[]?a,?int?start,?int?len,?int?key)?{??
  • ????int?high?=?start?+?len,?low?=?start?-?1,?guess;??
  • ????while?(high?-?low?>?1)?{??
  • ????????guess?=?(high?+?low)?/?2;??
  • ????????if?(a[guess]?<?key)??
  • ????????????low?=?guess;??
  • ????????else??
  • ????????????high?=?guess;??
  • ????}??
  • ????if?(high?==?start?+?len)??
  • ????????return?~(start?+?len);??
  • ????else?if?(a[high]?==?key)??
  • ????????return?high;??
  • ????else??
  • ????????return?~high;??
  • }??
  • 所以,它存儲的數值都是按鍵值從小到大的順序排列好的。

    添加數據:

    [java] view plaincopy
  • public?void?put(int?key,?E?value)?{}??
  • public?void?append(int?key,?E?value){}??

  • 刪除操作:

    [java] view plaincopy
  • public?void?delete(int?key)?{}??
  • public?void?remove(int?key)?{}???
  • public?void?removeAt(int?index){}??
  • public?void?clear(){}??

  • 修改數據:

    [java] view plaincopy
  • public?void?put(int?key,?E?value)??
  • public?void?setValueAt(int?index,?E?value)??

  • 查找數據:

    [java] view plaincopy
  • public?E?get(int?key)??
  • public?E?get(int?key,?E?valueIfKeyNotFound)??

  • 相應的也有SparseBooleanArray,用來取代HashMap<Integer, Boolean>,SparseIntArray用來取代HashMap<Integer, Integer>。

    SparseArray是android里為<Interger,Object>這樣的Hashmap而專門寫的類,目的是提高效率,其核心是折半查找函數(binarySearch)。當需要定義

    [java] view plaincopy
  • HashMap<Integer,?E>?hashMap?=?new?HashMap<Integer,?E>();??

  • 時,可以使用如下的方式來取得更好的性能。

    [java] view plaincopy
  • SparseArray<E>?sparseArray?=?new?SparseArray<E>();??

  • 參考資料

    http://developer.android.com/reference/android/util/SparseArray.html

    http://www.android-doc.com/reference/android/util/SparseIntArray.html


    http://blog.csdn.net/gogler/article/details/15810113

    http://www.cnblogs.com/KiloNet/p/3155658.html

    http://blog.csdn.net/kaixinbingju/article/details/8658428

    轉載于:https://www.cnblogs.com/duadu/p/6167317.html

    總結

    以上是生活随笔為你收集整理的【移动开发】SparseArray替代HashMap的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。