This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.