Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How implement data structure which is map many to one?

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.

enter image description here

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.

like image 555
user3487667 Avatar asked Nov 24 '25 01:11

user3487667


1 Answers

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.

like image 108
Adem Avatar answered Nov 25 '25 14:11

Adem