java - Storing multiple values with same key in HashMap -


i had interview today , interviewer asked me how can store multiple values having same key in hashmap? gave me example-->if given list of string , suppose store length of string key , string value.

i gave following solution in how using hashmap:

map<integer, arraylist<string>> map = new hashmap<integer, arraylist<string>>(); 

integer being length of string , arraylist store strings of particular length.

the interviewer said 1 way of using hashmap there way in won't requiring arraylist or other data structure. during interview, couldn't come solution , after enough googling, still have nothing. can tell me how can achieve solution question?

thanks!

one way without using data structure concatenating strings in values.

for e.g.

map.put(2,"rr*tt*yy"); map.put(3,"nnn*ggg*sss"); map.put(4,"ffff*dddd*jjjj"); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -