SparseArray vs HashMap

后端 未结 7 1706
-上瘾入骨i
-上瘾入骨i 2020-12-22 15:58

I can think of several reasons why HashMaps with integer keys are much better than SparseArrays:

  1. The Android documentation for a
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-22 16:30

    A sparse array in Java is a data structure which maps keys to values. Same idea as a Map, but different implementation:

    1. A Map is represented internally as an array of lists, where each element in these lists is a key,value pair. Both the key and value are object instances.

    2. A sparse array is simply made of two arrays: an arrays of (primitives) keys and an array of (objects) values. There can be gaps in these arrays indices, hence the term “sparse” array.

    The main interest of the SparseArray is that it saves memory by using primitives instead of objects as the key.

提交回复
热议问题