I have run across a number of cases where I have a Map where once I set it up, the keys never change (although the values against them do). I don't necessarily know what the keys will be until I create the map but once I do they are fixed.
I was wondering, is there a way to take advantage of this fact to design a Map like data structure that retrieves values more efficiently than the standard implementations?
The class signature would roughly follow the Map interface and might look something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class FixedKeyMap<K,V> { public FixedKeyMap(List<K> keys) {}
public void put(K key,V value) {}
public V get(K key) { }
public void remove(K key) { }
public void clear() {}
public Set<K> keySet() { } } |
I looked at the
HashMap source and it looks like you would be able to pull out a lot of code around load factors and capacities and simplify some of the other code because the size would be known at creation time.
Anyone have any thoughts on how such a thing might be designed to take advantage of the fact that the keys are all known up front? Could you get rid of the hashing function altogether?
I do recognize that this is a micro optimisation and may or may not be worth the effort for a given application but the question seemed interesting and so I thought I would ask if others had any ideas.