This week's giveaway is in the Spring forum.
We're giving away four 3 month subscriptions to Marco Behler Videos and have Marco Behler on-line!
See this thread for details.
Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

comparing keys of two hashmaps  RSS feed

 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I want to identify the identical keys amongst two haspmaps (H1 and H2).
Also I need to identify elements of H1 not available in H2 and vice versa.

Currently I am using the HashMap interface containsKey method but have to do something like this

- loop throught all keys of H1 and H2.containsKey(h1_key)
- loop throught all keys of H2 and H1.containsKey(h2_key)

Ist there a better view to implement the same.


Thanks


 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use the Google Collections package, it has functions to do the usual intersection, union, and difference functions for sets.
 
Vivek K Singh
Ranch Hand
Posts: 85
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need to loop thru both the maps, you can do something like:



This will get all keys from the hashmap1 and compare to the hashmap2 and print duplicates...
 
Rob Spoor
Sheriff
Posts: 21021
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use keySet() in combination with the bulk operators:
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!