我是Android的新手.在我正在使用的Android应用程序中HashMap,但我收到警告:
**"Use new SparseArray<String>(...) instead for better performance"**
Run Code Online (Sandbox Code Playgroud)
这意味着什么,我该如何使用SparseArray<String>呢?
Bha*_*iya 116
使用new
SparseArray<String>(...)代替更好的性能
由于此处描述的原因,您收到此警告.
SparseArrays将整数映射到对象.与普通的对象数组不同,索引中可能存在间隙.它旨在比使用HashMap将整数映射到对象更有效.
现在
我如何使用SparseArray?
您可以通过以下方式实现:
HashMap 办法:
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);
Run Code Online (Sandbox Code Playgroud)SparseArray 办法:
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);
Run Code Online (Sandbox Code Playgroud)希望它会有所帮助.
Nic*_*son 42
SparseArray当您使用Integer键作为键时使用.
当使用时SparseArray,键将始终作为基本变量保留,这与使用HashMap需要具有键Object作为键的位置不同,这将导致int在Integer短时间内成为对象,同时获取对象地图.
通过使用SparseArray您将保存垃圾收集器一些工作.
所以就像使用一样Map<Integer,String>.