I know this is possible:
Map<Integer, Object> map = new HashMap<Integer, Object>();
...
List<Object> arrayList = new ArrayList<Object>(map.values());
But according to android SparseArray<Object>
is more efficient, hence, I am wondering if it is possible to convert a SparseArray
to Arraylist
.
Much appreciate any input.
This will get just the values, ignoring gaps between indices (as your existing Map solution does):
public static <C> List<C> asList(SparseArray<C> sparseArray) {
if (sparseArray == null) return null;
List<C> arrayList = new ArrayList<C>(sparseArray.size());
for (int i = 0; i < sparseArray.size(); i++)
arrayList.add(sparseArray.valueAt(i));
return arrayList;
}
ArrayMap looks like a better choice, which is available since API 19.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With