• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashmaps; Print out only those keys with value.equals (true)  RSS feed

 
David Ward
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! I'm so far fairly impressed with myself not having to ask many questions here (partly due to the fact I've found a lot of answers already asked here!) this one has me slightly stumped though.

I have a hashmap <String, Boolean> and only wish to print out the key Strings which have the value true. The strings are all added initially with the value false, then if they re-occur in my .txt file the value is changed to true.

I was trying to go along the lines of something like...


I don't have the original code I had for this as I scrapped the class and started again hoping for a fresh perspective. So to sum up, how can I print only those keys with the appropriate value? Oracle doesn't give me any clues with this.

Thanks in advance. ;p
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like:
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Map interface has a method entrySet() which returns a Set of type Entry<K, V>. You can iterate over that and if the value is true, print the key.

But please, don't evaluate a Boolean with a comparison.
David Ward wrote:

Instead:

 
David Ward
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys, although I'm still unsure as to how to exactly implement the advice?

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Ward wrote:Thanks for the replies guys, although I'm still unsure as to how to exactly implement the advice?

Well Steve has given you the code. Which bit of it don't you understand ?
 
David Ward
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I was mis-using the code Steven gave but now I'm thinking my IF statements are missing the point! I'll have a break for tonight and go back through it tomorrow.
 
David Ward
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah it was my IFs never being false which threw my code. Many thanks folks and Steve in particular!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!