Warning shows when i use Hash Map In android(Use new SparseArray<String>)

20,148

Solution 1

Use new SparseArray<String>(...) instead for better performance

You are getting this warning because of reason described here.

SparseArrays map integers to Objects. Unlike a normal array of Objects, there can be gaps in the indices. It is intended to be more efficient than using a HashMap to map Integers to Objects.

Now

how i use SparseArray ?

You can do it by below ways:

  1. HashMap way:

    Map<Integer, Bitmap> _bitmapCache = new HashMap<Integer, Bitmap>();
    private void fillBitmapCache() {
         _bitmapCache.put(R.drawable.icon, BitmapFactory.decodeResource(getResources(), R.drawable.icon));
         _bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt));
         _bitmapCache.put(R.drawable.wallpaper, BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper));
         _bitmapCache.put(R.drawable.scissors, BitmapFactory.decodeResource(getResources(), 
     }
    
    Bitmap bm = _bitmapCache.get(R.drawable.icon);
    
  2. SparseArray way:

    SparseArray<Bitmap> _bitmapCache = new SparseArray<Bitmap>();
    private void fillBitmapCache() {
         _bitmapCache.put(R.drawable.icon, BitmapFactory.decodeResource(getResources(), R.drawable.icon));
         _bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt));
         _bitmapCache.put(R.drawable.wallpaper, BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper));
         _bitmapCache.put(R.drawable.scissors, BitmapFactory.decodeResource(getResources(), 
     }
    
    Bitmap bm = _bitmapCache.get(R.drawable.icon);
    

Hope it Will Help.

Solution 2

SparseArray is used when you are using an Integer as a key.

When using the SparseArray, the key will stay as a primitive variable at all times unlike when you use the HashMap where it is required to have a Object as a key which will cause the int to become an Integer object just for a short time while getting the object in the map.

By using the SparseArray you will save the Garbage Collector some work.

So use just like a Map<Integer,String>.

Solution 3

It's a hint that there is a better data structure for your code.

That hint is from Lint. You usually get it when you have a HashMap of integers to something else.

Its biggest advantage is to treat the integer key as a primitive. In other words, it won't covert to an Integer (the Java object) to use it as a key.

This is a big deal when using large maps. HashMap will result in the creation of many, many Integer objects in that case.

See a few more pieces of information here.

Share:
20,148
Naveen Kumar
Author by

Naveen Kumar

Updated on July 08, 2022

Comments

  • Naveen Kumar
    Naveen Kumar almost 2 years

    I am new to developing in android. In my android app I'm using HashMap, but I'm getting a warning:

    **"Use new SparseArray<String>(...) instead for better performance"**
    

    What does this mean, and how can I use SparseArray<String> instead?

  • orip
    orip about 11 years
    +1, avoids the autoboxing (and associated objects+GC) that happens with HashMap. @cyril-mottier wrote about it here: speakerdeck.com/cyrilmottier/…
  • Richard Le Mesurier
    Richard Le Mesurier almost 9 years
    Technically you are not "using an Integer as a key". You are using an int.
  • Kevin Mark
    Kevin Mark about 8 years
    Make certain to see the linked documentation which goes on to detail that the "implementation is not intended to be appropriate for data structures that may contain large numbers of items." A SparseArray is generally slower than a comparable HashMap.
  • The incredible Jan
    The incredible Jan almost 7 years
    developer.android.com/reference/android/util/SparseArray.htm‌​l says: " 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, ..." - that means: if you are interested in speed and want to store many values you have to use HashMap.
  • Magic Marbles
    Magic Marbles over 5 years
    I'd read the docs, but still had some questions and read a bit about Java maps as well. Somethings I found on HashMap: It provides constant-time performance for get/put, IF the hash function disperses "properly". Map permits a null key (table does not). On sparse arrays, docs say better performance with small sets of data. What size? (quote) "For containers holding up to hundreds of items, the performance difference is less than 50%" -- I take that to mean sparse begin losing out to map(/table?) when working with hundreds of key:values. In my case - I wanted sparse.