日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【移动开发】SparseArray替代HashMap

發布時間:2025/3/15 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【移动开发】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的全部內容,希望文章能夠幫你解決所遇到的問題。

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