Question

So, I'm supposed to use SparseArray instead of HashMap for the sake of performance:

However, SparseArray isn't a part of JCF and does not implement Collection nor List nor Map. HashMap, on the other hand, implements Map and provides values() that I can work with when needing JCF-compatible behavior. For example, using it in an ArrayAdapter and various custom sorting (for values).

My question is three-fold:

  1. Why doesn't SparseArray implement JCF interfaces? I mean, what is the motivation for not implementing these interfaces in light of the fact that most of the methods are already there?
  2. Are there alternatives to SparseArray that implement JCF interfaces or can easily be converted and retain the SparseArray performance?
  3. Are HashMaps with a few hundred elements really that much slower? Are my users really going to notice?

I'm looking for answers with depth and I prefer to have references to authoritative sites. If you think you know why SparseArray was not implemented with JCF interfaces, show some support, help me understand. If you think I should be using SparseArray, show me how to use it with ArrayAdapter and custom sorting (Comparator-esque solutions preferred). If there are better alternatives, links to the API doc, library or tutorial would be helpful. If you think I should stick with HashMaps, explain why the performance benefit of SparseArray is outweighed by the needs of the interfaces.

Was it helpful?

Solution

The first version of SparseArray.java was written in May 2006. In some ways it pre-dates the various collection interfaces you're referring to (within Android, not Java). My sense is that it doesn't support the various JCF interfaces because they didn't exist at the time the code was written, and nobody has found reason to add them since. (It didn't even use generics until early 2007.)

If you would find an enhanced SparseArray useful, high-quality patches are always welcome.

OTHER TIPS

Maybe because it's Map < Object, Object > but SparseArray has primitive keys.

Alternative is Map as it was before

For 3, in general, yes, but one should measure and see if it's true in his case

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top