Link to home
Start Free TrialLog in
Avatar of awking00
awking00Flag for United States of America

asked on

How can I best compare two HashMaps with similar keys to get entry sets that are in one but not the other?

I have two HashMaps created from two different hierarchal file structures, one representing Excel files and the other schema definitions. For example,
Map<String, String> xlsMap = new HashMap<String, String>();
Map<String, String> xsdMap = new HashMap<String, String>();
I have a class that populates each with keys that are an identifying name that is common to both. In both cases, the values are the full path and filename where the spreadsheets and schemas reside. Assuming sample values like the following:

xlsMap.put("Parent1","C:\user\documents\Spreadsheets\Parents\Parent1.xls");
xlsMap.put("Child1","C:\user\documents\Spreadsheets\Parents\Dependents\Children\Child1.xls");
xlsMap.put("Child2","C:\user\documents\Spreadsheets\Parents\Dependents\Children\Child2.xls");
xlsMap.put("Dog1","C:\user\documents\Spreadsheets\Dependents\Other\Dog1.xls");
xlsMap.put("Cat1","C:\user\documents\Spreadsheets\Dependents\Other\Cat1.xls");

xsdMap.put("Parent1","C:\user\documents\Schemas\2017v1.0\Parents\table1.xsd");
xsdMap.put("Child1","C:\user\documents\Schemas\2017v1.0\Parents\Dependencies\Child1.xsd");
xsdMap.put("Child2","C:\user\documents\Schemas\2017v1.0\Parents\Dependencies\Child2.xsd");
xsdMap.put("Dog1","C:\user\documents\Schemas\2017v1.0\Dependencies\Other\Dog1.xsd");
xsdMap.put("Cat1","C:\user\documents\Schemas\2017v1.0\Dependencies\Other\Cat1.xsd");
xsdMap.put("Cat2","C:\user\documents\Schemas\2017v1.0\Dependencies\Other\Cat2.xsd");


This indicates a new table ("Table1Cat2") is needed. I have another class, xlsGen, that will generate a new spreadsheet given the version of the xsd, the filename of the xsd, and the destination directory for the spreadsheet. So for this small example, I would like to: 1) determine when a new table is needed; and, 2) create an ArrayList of xlsParameters that I can pass to the xlsGen.createSpreadsheet method. So the ArrayList would look like
{["2017v1.0","C:\user\documents\Schemas\2017v1.0\Dependencies\Other\Cat2.xsd","C:\user\documents\Spreadsheets\Dependents\Other"]}. In this case there is only one but there could be numerous new tables. I think I could probably get this done by iterating through the schema file structure one at a time then iterating through the spreadsheet file structure to determine the schemas that don't match. However, there are currently 240 some tables that would mean 60,000 iterations and I'm sure there must be an easier way. As always, I appreciate any help the experts might provide.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

http://docs.oracle.com/javase/8/docs/api/java/util/AbstractSet.html#removeAll-java.util.Collection-

Something like this might get you started

Set<String> copyXsdKeys= new HashSet<>(xsdMap.keySet());
Set<String> newKeys= copyXsdKeys.removeAll(xlsMap.keySet());

Open in new window

Avatar of awking00

ASKER

CEHJ, Thanks for your prompt reply. I'm kind of tied up for the moment but will take a look at what you have shown as soon as I can. My first thought is that the resulting newKeys set would only contain the remaining keyset from the xsdMap, but I'm not sure how that provides me with the excel file destination needed to generate a new spreadsheet.
I don't understand enough of how your thing works to say. I don't even know how you got 'Table1Cat2' above, but i've shown you how to get the Cat2 part ;)
CEHJ,
Sorry to take so long getting back to you. It took me a while to populate the two maps as other tasks got in the way. The above should have just said Cat2, but I am still unable to get that. The following code produces an error that says cannon convert from Boolean to Set<String>
Set<String> xsdKeys = new HashSet<String>(xsdMap.keySet());
Set<String> newKeys = xsdKeys.removeAll(xlsMap.keySet());  ==> here
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CEHJ,
Thanks a lot. I should have figured that out. This, at least, gets me through the first step of a fairly complicated process that I need and answers my original question. I may have some more questions as I progress that I will post as needed.
Thanks again.