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);
来源:oschina
链接:https://my.oschina.net/u/855521/blog/681190