0

so I've been trying to write a method that takes a HashMap, and counts how many of the same keys there are in the HashMap. So here's the code, it should be pretty self explanatory.

import java.util.HashMap;
import java.util.Map;

public class ExtendedHashMap extends HashMap<String, Integer> {

public ExtendedHashMap() {
    super();
}

public int keyCount(String keyString) {
    String key = keyString;
    int keyCountInt = 0;
    for(Map.Entry<String, Integer> entry : this.entrySet()) {
        if(entry.getKey() == keyString) {
            keyCountInt++;
        }
    }
    return keyCountInt;
}

public static void main(String[] args) {
    ExtendedHashMap ex = new ExtendedHashMap();
    ex.put("Item One", 5);
    ex.put("Item Three", 25);
    ex.put("Item Four", 35);
    ex.put("Item Two", 15);
    ex.put("Item One", 5);
    ex.put("Item Two", 15);
    ex.put("Item Three", 25);
    ex.put("Item Four", 35);
    System.out.println(ex.keyCount("Item One"));
}
}

If you run this, you'll see that it outputs one no matter what. If you notice, the keys only have one value, so it rules out that. How can I get the the values to output as it's supposed to?

Hathor
  • 187
  • 1
  • 2
  • 10

4 Answers4

2

Duplicate keys are not allowed in HashMap. So it is obvious you will get only one as the answer!

Just for the record you will not get any Excpetion when you add two different values with same key. Your original value will silently get replaced by the new value. Try doing

 ex.get("Item One");

You may want to do the same operation on Multimap.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
1

counts how many of the same keys there are in the HashMap.

The keys are unique in all maps. If the key is there in the Map the count will always be ONE Go trough the API of MAP first.

A map cannot contain duplicate keys; each key can map to at most one value.

Also Map.html#entrySet() returns a java.util.Set class which cannot contain no duplicate elements.

Jayamohan
  • 12,734
  • 2
  • 27
  • 41
1

HashMap is a key value pair data structure. and key is unique. So answer is one.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

I strongly recommend you wrap a collection instead of extending it.

This is the same as

public int keyCount(String keyString) {
    return containsKey(keyString) ? 1 : 0;
}

Perhaps you had a MultiMap in mind?

public int keyCount(String keyString) {
    Collection coll = multiMap.get(keyString)
    return coll == null ? 0 : coll.size();

}

However I suspect MultiSet is what you really want

import com.google.common.collect.ConcurrentHashMultiset;
import com.google.common.collect.Multiset;

public class Inventory {
    private final Multiset<String> countingSet = ConcurrentHashMultiset.create();

    public void add(String key, int count) {
        countingSet.add(key, count);
    }

    public int keyCount(String keyString) {
        return countingSet.count(keyString);
    }

    public static void main(String[] args) {
        Inventory ex = new Inventory();
        ex.add("Item One", 5);
        ex.add("Item Three", 25);
        ex.add("Item Four", 35);
        ex.add("Item Two", 15);
        ex.add("Item One", 5);
        ex.add("Item Two", 15);
        ex.add("Item Three", 25);
        ex.add("Item Four", 35);
        System.out.println(ex.keyCount("Wooden Sword"));
        System.out.println(ex.keyCount("Item Three"));
    }
}

prints

0
50
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Right, thanks. So what makes MultiMap different to where it CAN have more than one key present, and is there a core java solution to this? If not, I'll use Google Guava. Thanks for the help mate. – Hathor Sep 06 '13 at 12:01
  • 1
    I read your edit: I don't want to add the values of the key, I want to get how many occurrences there are of the same key. For instance, here, if it did what I wanted (based on your example) it would output 0 and then 2 (because there are 2 Item Three's in your example.) So that would be MultiMap, correct? – Hathor Sep 06 '13 at 12:08
  • @Hathor It would be MultiMap. – Peter Lawrey Sep 06 '13 at 13:36
  • 1
    Thanks for all the help mate! I spent this morning working on it, and I have it working. – Hathor Sep 07 '13 at 06:15