hashmap count

shragi used Ask the Experts™
I created hashmap as below

HashMap<String, String> h = new HashMap<String, String>();                          
h.put("xx", "156");
h.put("xx", "157");    
h.put("xx", "166");
h.put("yy", "176");    
h.put("yy", "179");

now if I want to know how many entries are there in the above hashmap for key "xx" what should I do...
so for above example the count for key "xx" is 3 and count for 'yy' is 2
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2013

There will be one value associated with each of the keys. The HashMap, HashTable, Map, etc. all work as a key=value.  When you first call h.put("xx", "156"); the value at "xx" key is "156". On each of the subsequent calls for the same key (h.put("xx", "157");   h.put("xx", "166");   ) the value for the "xx" key gets overwritten. So, after all your five calls the map will contain two elements. The element with the key "xx" will have a value of "166", and the element with key "yy" will have a value of "179". If you need to store multiple keys in a map you need to use a different collection
It's been awhile since I've worked with Java, so I looked up HashMap put Here is the excerpt:
public V put(K key, V value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
the previous value associated with key, or null if there was no mapping for key.
Based on this, I would expect that "166" is associated with "xx" and "179" is associated with "yy". Since the keys are distinct, there is only one value associated with one key.
Is it your intention to have multiple values associated with a single key? If so, take a look at the HashMap – Single Key and Multiple Values Example.
Their first example uses a List for the HashMap value.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Jeffrey Dake Senior Director of Technologyy
For what you are trying to do you would want to do a map that maps it's key to a set of values.   Something like...

HashMap<string, Set<string>> h = new HashMap<string, Set<string>>();

Open in new window

Then for each key you add you can create a new hashset to put your entries. Then you will get your map that keys on a single entry to multiple values.  Then you will be able to call get on your map and which will return your set of strings. Call size() on that and you have the count you expect
HashMap<string, Set<string>> h = new HashMap<string, Set<string>>();
quite similar to this line in the above link:
Map<String, List<String>> map   = new HashMap<String, List<String>>();
Top Expert 2016



awesome thanks for all the suggestions I missed the basic point of hashmap.... but I am good now...

I am using the below map and it solves my issue...
Map<String, List<String>> map   = new HashMap<String, List<String>>();

thanks to all again
Shragi -

Just pro-forma (as you shouldn't really be doing it), you could, if you wanted to, do this:

h.put("xx"+"-"+h.size(), "156");

Open in new window

. etc.,

and then interrogate the Map values with a positional method looking as far as the "-" from the left.

Either way, please don't forget to close your questions - that's important.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial