Difference between SparseArray Vs ArrayList?

后端 未结 2 1015
情歌与酒
情歌与酒 2021-02-01 16:02

I want to know performance and efficiency of SparseArray and ArrayList and which one is better to use. I can\'t understand when to use SparseArra

相关标签:
2条回答
  • 2021-02-01 16:18

    SparseList implements a SparseArray. This class is identical to java.util.ArrayList, except that the former permits assignment to array indexes that are beyond the current length of the list, using the expected set() and add() methods.*

    I think here there is all you need to know to understand which one to use.

    SparseList is also more efficient

    more info here

    0 讨论(0)
  • 2021-02-01 16:40

    The purpose of a SparseArray is to save memory if you have a list that has lots of gaps in. If you only have 10 items, and the numbers that index them range from 0 to 1000, then an ArrayList will have lots of null entries in it, and that will be quite wasteful. A SparseArray will use data structures internally to avoid that problem.

    The alternative in this situation is a HashMap, which is better than a SparseArray if you have lots of items.

    The implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashMap, since lookups require a binary search and adds and removes require inserting and deleting entries in the array. For containers holding up to hundreds of items, the performance difference is not significant, less than 50%.

    From the Android dev documentation.

    0 讨论(0)
提交回复
热议问题