Which java collection allows duplicate keys

29,556

Solution 1

You can do this with a multimap, using a set as the collection for the values, it is fairly simple to make.

Here is some of the basics of an implementation, not the whole thing but couldn't imagine you would need more than that or maybe a remove method

Edit

Just saw you wanted duplicate pairs to be thrown away, can do that using a set, instead of throwing an error just gave back the bool to show if it was there already or not (if it exists returns false)

public class MultiValueMap<K,V> 
{
    private final Map<K,Set<V>> mappings = new HashMap<K,Set<V>>();

    public Set<V> getValues(K key)
    {
        return mappings.get(key);
    }

    public Boolean putValue(K key, V value)
    {
        Set<V> target = mappings.get(key);

        if(target == null) 
        {
            target = new HashSet<V>();
            mappings.put(key,target);
        }

        return target.add(value);
    }

}

Solution 2

You cannot do it by Java collection.

You can use Multimap it supports duplicate keys but it also support duplicate keys and value pairs.

Best solution for you is use Multimap and check if value already exist then dont add it.

Solution 3

To my knowledge, there is no such collection implementation in the default JRE. However, there seem to be implementations in third party libraries.

To get something similar, you can use a Map<K, List<V>>, which is a map containing a list of values for each key.

However, I do not think that you need this. To merge values for duplicate keys you can check whether the key already exists before you put a new key-value pair into the map.

  • if it already exists, replace the value by the merged old and new value
  • if it does not yet exist, just put the new key-value pair into the map.

Solution 4

Since Guava 2.0, there's a new map type, the SetMultimap, that you can use that I think fits your purposes exactly. It allows duplicate keys, but not duplicate key/value pairs. See the Guava documentation.

Share:
29,556
amal
Author by

amal

Updated on July 21, 2020

Comments

  • amal
    amal almost 4 years

    Im trying to write program to remove duplicate key-value pairs of a word list. However if key is duplicated with different value, that record should be allowed to add. Please help me to understand Which java collection would be solve this situation.

    • key1 aaaa
    • key2 bbbb
    • key3 cccc
    • key4 dddd
    • key2 bbbb - duplicate pair - not allowed
    • key1 hhhh - duplicate key - allowed
    • key5 gggg
    • key2 nnnn
  • rootExplorr
    rootExplorr about 8 years
    In your arrogance you forgot to give a valid reason for the down vote.
  • simonalexander2005
    simonalexander2005 about 5 years
    HashMap doesn't store duplicate values under the same key - you can only have one entry per key; and that entry will be replaced if you add another entry with the same key. If you want to store all entries for a given key, the value would have to be a collection which you add to; which make it a lot more complicated to maintain