I had an interview today and my interviewer asked me how can I store multiple values that have the same key in a HashMap? She gave me this example -> If I was given a String list, and I suppose to store the length of the String as a key and the string itself as a value.
I gave her the following solution on how I will use the HashMap:
Map<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
An integer that is the length of the string, and an ArrayList will store the strings of the appropriate length.
The interviewer said that this is one way to use HashMap, but there is another way that I will not need an ArrayList or any other data structure. During the interview, I could not come up with any solution, and now, after a sufficient search on Google, I still have nothing. Can someone tell me how I can achieve a solution to this issue?
Thanks!
source share