Finding number of values in a HashMap?

Viewed 17137

What is the best / most efficient way to find the total number of Values in a HashMap.

I do not mean the .size() method as it counts the number of keys. I want the total number of values in all the keys.

I want to do so as my key is a String, but my value is a List.

7 Answers
import java.util.HashMap;
public class Solution {  
    public static void main(String args[]) {
        int total = 0;
        HashMap<String,String> a = new HashMap<String,String>();
        a.put("1.","1");
        a.put("2.","11");
        a.put("3.","21");
        a.put("4.","1211");
        a.put("5.","111221");          
        for (String l : a.values()) {
            total ++;
        }
        System.out.println(total);       
    }
}
Related