I want to create a table with String keyvalues and integer data. Right now I am leaning towards creating a two-dimensional HashMap:
HashMap<String, HashMap<String, Integer>>
Is that going to be my fastest and most flexible option? I am using it to create a language model so there will be a lot of changes and frequent lookups. Also, is that the correct syntax? And how would I initialize it?
new HashMap<String, new HashMap<String, Integer>>
seems wrong to me for some reason but I can’t place why.
You could also define your map like this:
Where
Pairhas a reasonable definition (don’t forgetequalsandhashCode!).This is simpler (insert doesn’t have to check whether the sub-map is created or not) and probably faster (only 1 hash lookup instead of 2).
If you know of some character that can’t be in either of your strings, then you could just use
Stringinstead ofPair<String>where the key is the concatenation of the two keys with that character as a separator.