How implement data structure which is map many to one?
data-structures, dictionary, java, many-to-one
Solution
HashMap should be used. when you put "cloth" into HashMap as a value, it is not duplicated in memory. just reference is written into HashMap.
String hat = "hat";
String dress = "dress";
String paths = "paths";
String scarf = "scarf";
String cloth = "cloth";
HashMap h = new HashMap();
h.put(hat,cloth);
h.put(paths,cloth);
h.put(dress,cloth);
h.put(scarf,cloth);
for this sample, memory keeps only cloth object for once.
Problem
I want to have map in reverse direction which is mean I have lots of keys and only one value. I need this structure so when I search for one of the key I got the value. I can use a simple Hash Map but it waste space because of storing value many times. I am looking for optimize and efficient implementation in java. I appreciate your suggestion.