Question: 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?

Answer: 
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](http://guava-libraries.googlecode.com/svn/tags/release03/javadoc/com/google/common/collect/Multimap.html) 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

```
> 
> 
> > 
> > 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](http://docs.oracle.com/javase/6/docs/api/java/util/Map.html) first.

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

Also [Map.html#entrySet()](http://docs.oracle.com/javase/6/docs/api/java/util/Map.html#entrySet%28%29) returns a `java.util.Set` class which cannot contain no duplicate elements.