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

AndroidHashmap

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

>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 - Android

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 - Android

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.

Solution 4 - Android

SparseArray is better memory efficient Data Structure than HashMap for integer key value pairs. And it is specific to Android and I believe it can't be used for Java SE.

Sometimes, Linter warning shows that SparseArray must be used as in the above. This dumb warning might be right or wrong. You need to decide to use SparseArray rather HashMap if you find it suitable or you need to suppress it with @SuppressLint annotation.

To decide whether to implement, it is worthwhile doing a little research and evaluate your situation. You can find all some useful links here

https://gunhansancar.com/sparsearray-vs-hashmap/

https://developer.android.com/reference/android/util/SparseIntArray

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNaveen KumarView Question on Stackoverflow
Solution 1 - AndroidBhavesh PatadiyaView Answer on Stackoverflow
Solution 2 - AndroidNicklas Gnejs ErikssonView Answer on Stackoverflow
Solution 3 - AndroidChristian GarbinView Answer on Stackoverflow
Solution 4 - AndroidFarruh HabibullaevView Answer on Stackoverflow