9

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?

1
  • what is the benefit of doing this? what are you trying to accomplish, maybe there is a better way of doing this if you give some more deatils Commented Jan 29, 2011 at 17:50

6 Answers 6

13

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);
}
Sign up to request clarification or add additional context in comments.

Comments

13

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]

Comments

5

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.

Comments

3

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.

4 Comments

More specifically I think it would need to be a ListMultimap. A SetMultimap would not allow duplicate key/value pairs but there is one in the example input. That article is a bit out of date. Google Collections has since been merged into Guava.
Right. Or you can use HashMultimap
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.
Right. Okay I went back and closely looked at the OP's data .. he/she does have a duplicate key/value pair ... at first I thought he/she was just needing duplicate keys. Thanks for pointing it out :-)
0

You can't , you can however create your own implementation of map which allows duplicates inside.

1 Comment

If it allows duplicates, it's not a Map.
0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.