What is the Clear Purpose of SparseBooleanArray?? [ I referred official android site for this ]

前端 未结 4 1993
误落风尘
误落风尘 2021-01-08 00:40

I referred the android doc site for \"SparseBooleanArray\" class but still not getting idea of that class about what is the purpose of that class?? For what purpose we need

4条回答
  •  盖世英雄少女心
    2021-01-08 01:38

    There can be three ways to store resource id's

    1 Array

    Boolean array containing id's as indexes.If we have used that id set it to true else false

    Though all the operations are fast but this implementation will require huge amount of space.So it can't be used

    High Space Complexity

    2 HashMap

    Key-ID

    Value-Boolean True/False

    Using this we need to process each id using the hashing function which will consume memory.Also there may be some empty locations where no id will be stored and we also need to deal with crashes.So due to usage complexity and medium space complexity, it is not used.

    Medium Space Complexity

    3 SparseBooleanArray

    It is middle way.It uses mapping and Array Implementation

    Key - ID

    Value - Boolean True/False

    It is an ArrayList which stores id's in an increasing order.So minimum space is used as it only contains id's which are being used.For searching an id binary search is used. Though Binary Search O(logn) is slower than hashing O(1) or Array O(1),i.e. all the operations Insertion, Deletion, Searching will take more time but there is least memory wastage.So to save memory we prefer SparseBoolean Array

    Least Space Complexity

提交回复
热议问题