user2379090
user2379090

Reputation: 123

Counting duplicate values in Hashmap

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.

Upvotes: 2

Views: 24772

Answers (4)

Saravanan R
Saravanan R

Reputation: 21

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
            }

Upvotes: 1

Imdad Soomro
Imdad Soomro

Reputation: 1

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)

Upvotes: 0

nem035
nem035

Reputation: 35481

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"

Upvotes: 2

MartaGom
MartaGom

Reputation: 501

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,

Upvotes: 0

Related Questions