I want to add duplicate elements on hashmap
so:
put("name1", 1);
put("name1", 3);
put("name1", 3);
put("name2", 1);
put("name2", 3);
how i can do that?
解决方案
Use a Map> i.e. you map a string to a list of integers.
So, in this case, name1 would map to a list of [1,3,3].
Obviously you'd have to write your own put method, in which you add the int to the list. Example:
put(String s, int i){
List list = map.get(s);
if(list == null){
list = new ArrayList();
map.put(s, list);
}
list.add(i);
}