File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes comparing two HashMaps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "comparing two HashMaps" Watch "comparing two HashMaps" New topic

comparing two HashMaps

David Summers

Joined: Dec 16, 2002
Posts: 1
Hello and thank you in advance.........

I am writing an application for my company. I am wanting to compare inventory authorized to that of inventory on-hand. First, I have constructed a HashMap, HashMap authorized, containing serial-numbers(keys)and authorized quantities(values). Next, I am querying a database and obtaining a ResultSet object that contains serial numbers. I am wanting to take this ResultSet and make a HashMap, HashMap on_hand, containing the same serial numbers but with the quantities that are present.
Once I have these two HashMaps, I am wanting to compare the values(authorized vs. on-hand) for each serial number in the HashMap.
I know HashMap do not keep order. Is there a better way to do this.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
You can load both HashMaps, then iterate through the entries in one, and try to get() the entry for the same key in the other table. An example may be found in my first post in this thread. Note also that you don't necessarily have to wait for the second HashMap to be complete - since you're already looping through ResultSets, you could start your comparison as you build the second HashMap, by checking the values you read from the second ResultSet against the first HashMap. In fact, if the only reason you build the HashMaps is to generate the comparison, you may not even need to build the second HashMap at all.
Also: if you need to check for inventory that is authorized but not on-hand, you will need to loop through the authorized HashMap or ResultSet and check it against the on-hand HashMap. If you need to check for inventory that is on-hand but not authorized , you will need to loop through the on-hand HashMap or ResultSet and check it against the authorized HashMap. If you need to do both, you will have to build both HashMaps and loop through each.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: comparing two HashMaps
It's not a secret anymore!