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

后端 未结 4 1381
不知归路
不知归路 2021-01-30 03:01

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

**\"Use new SparseArray(...) ins         


        
4条回答
  •  遇见更好的自我
    2021-01-30 03:27

    Use new SparseArray(...) 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 _bitmapCache = new HashMap();
      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 _bitmapCache = new SparseArray();
      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.

提交回复
热议问题