I've been trying to use TreeMaps on Android and ran into the following two issues:
java.lang.NoSuchMethodError: java.util.TreeMap.lowerEntry
Basically, I need a data structure that provides a super-fast map, "indexed" by a sparse integer-key (e.g. it contains entries for keys 2, 100, 29392, 399391, ...). It needs to be able to quickly perform the following operations:
So, basically I need TreeMap's get
, put
, remove
, values
, clear
, ceilingEntry
or higherEntry
, and lowerEntry
or floorEntry
. (For the last two either option is ok as one can be turned into the other simply by incrementing or decrementing the reference key by 1)
Are there any alternatives to TreeMap that perform well and are available either on all Android devices or I can include in my app?
UPDATE: I need to apologize to Android or whoever was involved in building the TreeMap: Due to some stupid logic-mistake, I was calling the TreeMap methods a whole bunch more times than I thought. The performance is actually perfectly fine. I was just being retarded... So, point 2 no longer is a concern. Leaves point 1. Sorry, everyone...
You can emulate some of the NavigableMap
methods with some trickery. For example, ceilingEntry
is approximately equivalent to tailMap(key).entrySet().iterator().next()
, though it'll throw if there is no entry instead of returning null. (Just use hasNext()
on the iterator to work around that.)
lowerEntry
is harder; you can do headMap(key).lastKey()
and call get
on that to get the whole entry. higherEntry
and floorEntry
are harder still, but you've said that's not strictly necessary.