Efficient data structure for a record consisting of a tuple and its value
I want to store sort of a tuple and a value as an entry. I mean something like this
A B -> 1 A C -> 2 E F -> 3 C D -> 4
The first two values are always unique, in the sense A and B occur only once together. What would be an efficient data structure for the purpose, keeping in mind that I may need to fetch any of the three fields from an entry efficiently ?
If possible, please provide some relevant code !!
A Map would work well. You need to have an object to represent the first two items. Make sure that the hashCode and equals methods are implemented correctly on your new class. Then you can use that class for the key.
Map<MyKeyClass, Integer> map = new HashMap<MyKeyClass, Integer>(); map.put(new MyKeyClass("A", "B"), 1); map.put(new MyKeyClass("A", "C"), 2);