Counting duplicate values in Hashmap

20,055

Solution 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"

Solution 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
            }
Share:
20,055
user2379090
Author by

user2379090

Updated on July 09, 2022

Comments

  • user2379090
    user2379090 almost 2 years

    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.

  • dragon66
    dragon66 over 9 years
    why new String("Rooney")? what's wrong with "Rooney"?
  • MartaGom
    MartaGom over 9 years
    Both can be used without problem: I like to create the object String, because in the HashMap it is defined as the String class. And I prefer it, that's all :)
  • ajb
    ajb over 9 years
    Using a string literal "Rooney" will also create a String object. All you've done is create a wasted duplicate object.
  • Tom
    Tom over 9 years
    You prefer to create duplicate String Objects? Mhh, ok. Its like prefer using raw types instead of generics.
  • nem035
    nem035 over 9 years
    @MartaFernandez, you might have some things confused, check out this link String Object vs Literal
  • MartaGom
    MartaGom over 9 years
    I wasted? By using "new" Java is not responsible for creating the object as you would the other way. Remember that String is not a primitive data in java.
  • Tom
    Tom over 9 years
    The difference between new String("Test") and "Test" is, that the first one will always create a new instance of String. The latter will create a new instance for the first call and intern that instance. If you call "Test" a second time, then you will get that instance, instead of a completely new one. Therefore, your system doesn't have to store several String instances containing the same value.
  • MartaGom
    MartaGom over 9 years
    Looks like I was wrong. I seen the link of Nem and I read the comment of Tom. Sorry for my confusion... x_x And thank you for the correction
  • André Kool
    André Kool about 8 years
    Please add some explenation to your answer.
  • Saravanan R
    Saravanan R about 8 years
    Output:If you print your newHm you get "one"-3, "two"-2.