0

I have a HashMap in Java:

HashMap<String, Integer> meh = new HashMap<String, Integer>();`

meh.put("one", 1);
meh.put("one", 1);
meh.put("one", 1);
meh.put("two", 1);
meh.put("two", 2);
meh.put("three", 3);

What I need is to remove duplicating entries ("one", 1) [when both key and value duplicate]. I searched and found only 'how to remove duplicating keys/values'. Can anyone help?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 2
    It is already done... you cannot have more than one entry with the same key... – SJuan76 Oct 07 '14 at 19:32
  • 1
    are you sure you can have duplicate keys? Check the java docs first... – Alboz Oct 07 '14 at 19:32
  • 1
    Maps will not let you have entry with same key more than once. Like in real world, point on map can describe only one location, so each time you use `put(key, value)` with same key you are only updating existing value. Only way I can think of for map to store more than one entries with same key is when instance of key is of a type which has incorrectly overridden hashcode and equals methods, which is impossible for keys of type String. – Pshemo Oct 07 '14 at 19:33
  • 1
    Duplicate of: http://stackoverflow.com/questions/17812454/remove-duplicate-values-from-hashmap-in-java – Ascalonian Oct 07 '14 at 19:33
  • 1
    @Ascalonian: That's not a duplicate, that question has no duplicate keys. – Keppil Oct 07 '14 at 19:34
  • Yes, I do have duplicaing entries, because I put values into HashMap from a database. – user3605970 Oct 07 '14 at 19:35
  • `Map`s cannot contain duplicate keys, regardless of the associated value. That's one of their a central aspects. – John Bollinger Oct 07 '14 at 19:35
  • @user3605970 the point is that a Hashmap cannot have duplicate keys! it may have duplicate values with different keys, but not duplicate keys. – Alboz Oct 07 '14 at 19:36

4 Answers4

3

There is no need to do that, HashMap takes care of that automatically. What happens when you execute that code is essentially as follows:

meh.put("one", 1);

this makes the map {"one" -> 1}

meh.put("one", 1);

this replaces the assignment by itself, making the map {"one" -> 1}

meh.put("one", 1);

this replaces the assignment by itself, making the map {"one" -> 1}

meh.put("two", 1);

this adds the requested linking, making the map {"one" -> 1, "two" -> 1}

meh.put("two", 2);

this replaces the assignment for "two", making the map {"one" -> 1, "two" -> 2}

meh.put("three", 3);

this adds the new element, making the total mapping {"one" -> 1, "two" -> 2, "three" -> 3} with no duplicates involved.

user1111929
  • 6,050
  • 9
  • 43
  • 73
2

You cannot remove duplicates from a HashMap because there are no duplicates in a HashMap in the first place. The second (or third, or whatever) time you call put with a key that already exists in the map, it will simply override the value with a new one, regardless of whether it's a duplicate or not of the pre-existing one.

In the code snippet you provided, the map will only have three values.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

From what you describe, Map<String, Integer> is not the structure adapted to your needs: you do not seem to assiciate the value 2 to "two" but you are actually storing pairs of elements.

A better data structure might be to use a Set of MyPair where MyPair is:

public class MyPair {
    private String first;
    private int second;
    // + constructor, getters + setters, hashcode + equals
}

And then you can use that MyPair object in a HashSet:

Set<MyPair> myPairs = new HashSet();
myPairs.add(new MyPair("one", 1));
myPairs.add(new MyPair("one", 2));
myPairs.add(new MyPair("two", 2));    
myPairs.add(new MyPair("two", 2));
myPairs.remove(new MyPair("one", 2)); // remove MyPair("one", 2) only
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • Beat me by four seconds, plus I like your explicit observation that OP is really just storing pairs of elements. I'm leaving mine because OP's use of a hashmap suggests there might be a need to extract all the unique values mapped to by a key. – CPerkins Oct 07 '14 at 19:47
1

Your real question isn't how to remove duplicates.

It's how to preserve the unique values with duplicate keys.

See this answer https://stackoverflow.com/a/8229534/152578 by Jon Skeet for details, but basically you're looking for a multimap.

You'll have to check for the value on insertion.

Another choice would be to unite the key and value (method depending on your symbol space), and store them in a set.

Community
  • 1
  • 1
CPerkins
  • 8,968
  • 3
  • 34
  • 47