Counting duplicate values in Hashmap

浪子不回头ぞ 提交于 2020-01-02 10:07:43

问题


I have a hash map that looks like this:

HashMap<String, ArrayList<String>> varX = new HashMap<String, ArrayList<String>>();

And I can't for the life of me work out how to count the number of duplicate values. For example, If put("001", "DM"); into the hash map and put("010", "DM"); as well, how can count if there are two values int the ArrayList section of the Hashmap.

For example, the output would look something like this:

DM:2 as I 'put' two DM values into the Hashmap.


回答1:


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"



回答2:


HashMap hm =new HashMap();
hm.put("01","one");
hm.put("02","two");
hm.put("03","one");
hm.put("04","one");
hm.put("05","two");
HashMap newHm = new  HashMap();
         Iterator it = hm.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry)it.next();
                System.out.println(pair.getKey() + " = " + pair.getValue());
                if(newHm.containsKey(pair.getValue())){
                    newHm.put(pair.getValue(), Integer.parseInt(newHm.get(pair.getValue()).toString())+1 );
                }else{
                    newHm.put(pair.getValue(),1 );
                }
                it.remove(); // avoids a ConcurrentModificationException
            }



回答3:


As Sotirios says, you can only put an ArrayList.

ArrayList<String> names= new ArrayList<String>();
names.add("Josh");
put("001", names);

If you want to insert Strings into the HashMap, define it as follow:

Map<String,String> example = new HashMap<String,String>();
example.put( "001", new String( "Rooney" ));

Regards,




回答4:


Collections.frequency(map, "value"); is used to count the passed object in collection. Following is the declaration of that method:

public static int frequency(Collection<?> c, Object o)


来源:https://stackoverflow.com/questions/27254302/counting-duplicate-values-in-hashmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!