You have a HashMap that maps String
to ArrayList<String>
.
Doing put("001", "DM")
on this map will not work as was pointed out to you in the comments by @Sotirios Delimanolis.
You would get an error that looks like:
The method put(String, ArrayList<String>) in the type HashMap<String,ArrayList<String>> is not applicable for the arguments (String, String)
Based on your example behavior, you want a HashMap
that maps String
to String
(i.e. put("001", "DM");
Now, assuming you have that:
HashMap<String, String> varX = new HashMap<String, String>();
And you want to count how many keys map to the same value, here's how you can do that:
varX.put("001", "DM");
varX.put("010", "DM");
// ...
int counter = 0;
String countingFor = "DM";
for(String key : varX.keySet()) { // iterate through all the keys in this HashMap
if(varX.get(key).equals(countingFor)) { // if a key maps to the string you need, increment the counter
counter++;
}
}
System.out.println(countingFor + ":" + counter); // would print out "DM:2"