aspose file tools*
The moose likes Java in General and the fly likes Iterating through Key only on hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Iterating through Key only on hashmap" Watch "Iterating through Key only on hashmap" New topic
Author

Iterating through Key only on hashmap

Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
I'm trying to iterate through a hashmap and print out the ArrayList that is affiliated with each key. I have a hashmap that takes in a object and an arraylist. The object is the key. The way I'm currently doing it, it seems to be printing out a big list, but the map size is only 3.. What am I doing wrong? There should only be an ArrayList per each key.. And in my Class, I have an equals function and hashcode function to check for duplicate key. If it exists I just add to the arraylist. If it doesn't I add the new key, and a new arraylist.

here is my iteration function:

Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

Well, first things first - please make sure that in reality there are only 3 keys in the Map. You can easily figure this out during debugging (or by 'size' method of HashMap).

Secondly, if you are putting more than 3 keys and because some are duplicate, you are thinking that there should be actually 3 unique keys, then have a close look at hashCode and equals methods of ConflictId class. Make sure that those methods do follow the proper contract(s).

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Thanks for the help.

Yea I did the size method on Map that's how I was able to determine the actual size.
I did look at the methods inside the ConflictId and did a printout to make sure it's being called and it is.
I just don't understand why I'm getting a bunch more output... If you can think of anything else let me know.. Thank you.
Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
I actually figured it out this morning at my desk what I was doing wrong. Inside another function where I call my iteration function I was calling inside a loop where I had thought it was outside... So that was the reason for the multiple output.

Thanks for the assistance.
 
 
subject: Iterating through Key only on hashmap