Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 925
Naveen Kumar Avatar asked Feb 09 '13 11:02

Naveen Kumar


People also ask

What is SparseArray in Java?

SparseArray maps integers to Objects and, unlike a normal array of Objects, its indices can contain gaps. SparseArray is intended to be more memory-efficient than a HashMap , because it avoids auto-boxing keys and its data structure doesn't rely on an extra entry object for each mapping.

What is the difference between ArrayMap and HashMap?

ArrayMap is a generic key -> value mapping data structure that is designed to be more memory efficient than a traditional HashMap. ArrayMap keeps its mappings in an array data structure — an integer array of hash codes for each item, and an Object array of the key -> value pairs.

Why do we use HashMap in Android?

A HashMap is a structure allowing one to store (key,value) items. A hash function pairs each key to an array index where the value will be stored.

What is HashMap in Java?

The HashMap class of the Java collections framework provides the functionality of the hash table data structure. It stores elements in key/value pairs. Here, keys are unique identifiers used to associate each value on a map. The HashMap class implements the Map interface.


1 Answers

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.

like image 177
Bhavesh Patadiya Avatar answered Sep 28 '22 12:09

Bhavesh Patadiya