9

I'm trying to write a program to remove duplicate key-value pairs of a word list.

However, a key duplicated with a different value should be allowed to be added.

Which Java collection would 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
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
amal
  • 3,470
  • 10
  • 29
  • 43
  • keys cannot be duplicate by definition. What you are talking about is called a compound key (made up of several values). So this means you can use a regular map as long as you use a key that combines both values. – Dmitry B. Feb 26 '16 at 01:29

6 Answers6

9

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

}
konkked
  • 3,161
  • 14
  • 19
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.

sanky jain
  • 873
  • 1
  • 6
  • 14
2

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.
Community
  • 1
  • 1
Stefan Dollase
  • 4,530
  • 3
  • 27
  • 51
1

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.

Brian
  • 21
  • 5
-1

A dictionary contains words and definitions, so sheep="wooly mammal" is a valid assignment. Each time you look up sheep you get wooly mammal.

An array is indexed by an integer, and can have duplicate values,

    arr[2]=5 ; arr[7]=5;

A hash can also store duplicate values, but the keys must be unique:

    Adam{age}=21;
    Bill{age}=21;

Some languages use dots for properties:

Adam.age=21;
Arif Burhan
  • 507
  • 4
  • 12
-2

Your case basically needs a HashMap.

Just put key as key and value as value in a HashMap.

This is because key will any ways be unique and in case there is collision in values HashMap maintains a linked list for storing all these colliding values.In case any value is same as any earlier value in the linked list it simply replaces the old one with the new one.

For ex.

As per your requirement :

Key1 aaaa -- should be stored Key1 bbbb -- should be stored Key1 aaaa -- should not be stored as it is duplicate.

So basically hashmap will store "aaaa" and "bbbb"values against "key1" as the key. Later when we try storing "aaaa" again against "key1" then older stored value "aaaa" will simply be replaced.

Hence, duplicacy of values is automatically handled by hashmap.

Hence , you can make use of HashMap in your case.

rootExplorr
  • 575
  • 3
  • 17
  • In your arrogance you forgot to give a valid reason for the down vote. – rootExplorr Feb 29 '16 at 14:17
  • 2
    `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 – simonalexander2005 Jan 29 '19 at 11:17