I am storing some objects in map (hashed by strings), but the objects are categoriseable by another string.
I could therefore make a map of these categories, and for each category keep another map of the objects in that category.
I will always know the category whenever I make an insertion or a get request from this data structure.
Is this more efficient? It seems like it would be, except the lookup time of a map is log(n) I beleive, so what would the overall benefits be?
Doing the arithmetic, if you have n elements lookup time would be O(log(n)). If you split the map into m1 maps containing maps of size m2, then you need O(log(m1)) to lookup in the first map, then O(log(m2)) to do a lookup on the second map. But since
You aren’t buying much either way and you should probably code it whatever way best reveals your intentions.
As far as the actual performance, you should profile it and see if it makes a difference. It’s possible that the two maps will be faster since each map will have a simpler comparator functions (they will each do one string comparison while the comparator for the monolithic map will have to do two for some pairs objects). Also, if in your application you are likely to be not finding a match on the first string for a lot of your lookups then you will doing just first lookup which might save some time.
On the other hand, if the sizes of the secondary maps are not uniform, then you could be effectively be doing two lookups. As a worst-case consider that half the keys have identical first strings, and the other keys all have district strings.