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?