This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterating through Key only on hashmap

 
Joseph Cho
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joseph Cho
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic