Android 应用性能优化-ArrayMap、SparseArray

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

若是有如下场景的代码使用,Android建议咱们替换新的容器API。app

应用场景1code

  • 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");

应用场景2class

  • 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);
相关文章
相关标签/搜索