2

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.

nem035
  • 34,790
  • 6
  • 87
  • 99
user2379090
  • 125
  • 1
  • 3
  • 11
  • 9
    You can't `put("001", "DM");`. `"DM"` is not an `ArrayList`. What do you actually mean? – Sotirios Delimanolis Dec 02 '14 at 16:32
  • 1
    Are you fine with using a second HashMap to count? – Compass Dec 02 '14 at 16:35
  • 1
    You should definitly check [Guava Multimap](https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multimap). It stores a `Collection` as the value, but you don't have to care about that. You can simply write `multiMap.put("DM", "123");`. And mind, that you should switch your *key* and *value*. `DM` should be the *key*. – Tom Dec 02 '14 at 16:52

4 Answers4

2

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"
nem035
  • 34,790
  • 6
  • 87
  • 99
1
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
            }
0

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,

MartaGom
  • 501
  • 6
  • 27
  • 2
    why new String("Rooney")? what's wrong with "Rooney"? – dragon66 Dec 02 '14 at 16:41
  • 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 :) – MartaGom Dec 02 '14 at 16:48
  • Using a string literal `"Rooney"` will also create a `String` object. All you've done is create a wasted duplicate object. – ajb Dec 02 '14 at 16:53
  • 1
    You prefer to create duplicate String Objects? Mhh, ok. Its like prefer using raw types instead of generics. – Tom Dec 02 '14 at 16:54
  • @MartaFernandez, you might have some things confused, check out this link [String Object vs Literal](http://stackoverflow.com/a/18635326/3928341) – nem035 Dec 02 '14 at 16:56
  • 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. – MartaGom Dec 02 '14 at 16:57
  • 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](http://stackoverflow.com/questions/10578984/what-is-string-interning) 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. – Tom Dec 02 '14 at 17:00
  • 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 – MartaGom Dec 02 '14 at 17:06
0

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)