Say I have 3 sets of string values:
fruit: apple, berry, banana
color: red, blue, orange
vehicle: car, plane, truck
I'm looking for the most efficient way with Java to retrieve the parent value for each set such as:
getParentValue("banana") ---> fruit
Solution 1:
create a bunch of if/else statements or switch case:
if (fruitSet.contains(elem)) {
return "fruit";
}
else if (colorSet.contains(elem)) {
return "color";
} ...
This yields an O(n) lookup, n being numbers of sets.
Solution 2:
Create a hashmap which stores every child to parent value,
Key/Value:
apple/fruit
berry/fruit
banana/fruit
red/color
blue/color
orange/color
...
This yields an O(1) lookup time, but generates a large hash map as it stores every key - for some reason this solution feels ugly.
I am looking for some opinions or other approaches which might be more elegant.