If you are mapping Java shorts to a few immutable objects, and it is often the case that a consecutive sequence of short keys (neighbors) map to the same value, it there some map structure that allows you to save more memory then a hashmap, while keeping a fast access speed (O(1) or O(log(N)))?
I could inverse the map, and I would use much less memory, but then I would have to go through every mapping to know if a specific short is mapped, and to what it is mapped (O(N)).
I suppose some kind of treemap could do that; maybe there is something like that in some collection library?
You can use a binary tree with one entry for each interval of shorts that map to the same value.
The key would be the start of the interval, while the data is the length of the interval plus the mapped objects.
Thus to find if given short is mapped you need to locate the node in the tree, with the highest key less than the given one (O(logn)) and check whether the given one falls within the interval this node represents.