• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing keys of two hashmaps

 
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: 4678
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
Pie
Posts: 20611
63
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic