Android 应用性能优化-ArrayMap、SparseArray

时间秒杀一切 提交于 2019-12-10 16:58:05

Android系统针对移动平台端,编写了一些高效的容器API,比如ArrayMap、SparseArray,今天我门来使用这个API。

如果有以下场景的代码使用,Android建议我们替换新的容器API。

应用场景1

  • HashMap<E, E>
HashMap<String, String> map = new HashMap<>();
map.put("A", "A");
map.put("B", "B");
map.put("C", "C");
// 替换成如下
ArrayMap<String, String> map = new ArrayMap<>();
map.put("A", "A");
map.put("B", "B");
map.put("C", "C");

应用场景2

  • HashMap<Integer, String>
HashMap<Integer, String> list = new HashMap<>();
list.put(1, "A");
// 替换成如下
SparseArray<String> list = new SparseArray<>();
list.append(1, "A");
  • HashMap<Integer, Boolean>
HashMap<Integer, Boolean> list = new HashMap<>();
list.put(1, true);
// 替换成如下
SparseBooleanArray list = new SparseBooleanArray();
list.append(1, true);
  • HashMap<Integer, Integer>
HashMap<Integer, Integer> list = new HashMap<>();
list.put(1, 1);
// 替换成如下
SparseIntArray list = new SparseIntArray();
list.append(1, 1);
  • HashMap<Integer, Long>
HashMap<Integer, Long> list = new HashMap<>();
list.put(1, 10L);
// 替换成如下
SparseLongArray list = new SparseLongArray();
list.append(1, 10L);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!