So I thought I could use a HashMap for this but it won't work as the first key won't be unique - what makes it unique in the data set is the combination of key 1 and key 2.
I thought about concatenating the keys into a string to force uniqueness, which I think should work, but I wanted to confirm there isn't any other way. To be clear, I was hoping for a data structure that I could...
- Look up the first key
- Look up the second key
- Access the double stored
This works in a nested HashMap but since the first key will not be unique it gets updated with the new second key and double in the next iteration.
From searching, it seems like one suggestion is to use Multimap from Apache's library but I don't want a list to be returned when accessing the first key. I ultimately just want the double by accessing the first key then the second key.
Any suggestions? Or should I try to get concatenation to work?
Sample code
if(map.get(first[1]) != null &&
map.get(first[1]).get(second[1]) != null) {
HashMap<String, Double> inner = map.get(first[1]);
inner.put(second[1], inner.get(second[1]) + 1.0);
} else {
map.put(first[1], new HashMap<>());
map.get(first[1]).put(second[1], 1.0);
}
The AND check isn't necessary actually - should just be the second condition