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?
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<String, List<Integer>> 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<Integer> list = map.get(s);
if(list == null){
list = new ArrayList<Integer>();
map.put(s, list);
}
list.add(i);
}
The ListMultimap interface from Guava may meet your requirements. It allows duplicate keys and duplicate key/value pairs.
ListMultimap<String, Integer> m =
ArrayListMultimap.create();
m.put("name1", 1);
m.put("name1", 3);
m.put("name1", 3);
m.put("name2", 1);
m.put("name2", 3);
System.out.println(m.get("name1")); // => [1, 3, 3]
System.out.println(m.get("name2")); // => [1, 3]
Also do you really need to preserve duplicate key/value pairs? If not then a HashMultimap may be sufficient (and more efficient.) If you insert the same entries into a HashMultimap you get:
System.out.println(m.get("name1")); // => [1, 3]
System.out.println(m.get("name2")); // => [1, 3]
Your idea violates the contract of the Map interface:
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
It'd understandably be confusing to the map when you ask:
map.get("name1")
It wouldn't know which value to get.
I'd use dogbane's solution of mapping each key to a list of Integers. In your example, you have possible duplicate values. If you don't want duplicate values (i.e. for "name1" there'd be only one 3 in the resulting list), you could instead make it a Map of Strings to Sets of Integers.
You should use Google Collection's Multimap data structure.
A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.
This is exactly what you are trying to achieve. No need to re-invent the wheel by writing your custom Map operations in my opinion. Also you may find this tutorial on MultiMap useful.
HashMultimap does allow duplicate keys but not duplicate key/value pairs. If you insert the example entries into a HashMultimap you end up with only one name1=3 entry.You can't , you can however create your own implementation of map which allows duplicates inside.
Map.org.apache.commons.collections.map.MultiValueMap is a more suitable choice.It can put one key to multiple values.
public Object put(Object key, Object value) {
boolean result = false;
Collection coll = getCollection(key);
if (coll == null) {
coll = createCollection(1);
result = coll.add(value);
if (coll.size() > 0) {
// only add if non-zero size to maintain class state
getMap().put(key, coll);
result = false;
}
} else {
result = coll.add(value);
}
return (result ? value : null);
}