Programming Tips - Android: "Use new SparseArray() instead for better performance"

Date: 2021oct26 Language: Java Q. Android: "Use new SparseArray<>() instead for better performance" A. You may get this message when compiling an Android project that uses HashMap<>. SparseArray<> is more memory efficient than HashMap<> is its generally preferred. We aren't going discuss all the trade-offs here. If you want to continue using HashMap<> put
@SuppressLint("UseSparseArrays")
In front of it to avoid the warning. How to change HashMap<> to SparseArray<> For example, change the declaration from:
HashMap<Integer, Status> mStatus = new HashMap<Integer, Status>();
to
SparseArray<Status> mStatus = new SparseArray<Status>();
And all uses are the same, eg:
mStatus.put(id, status); Status status = mStatus.get(id);