awking00
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:\u ser\docume nts\Spread sheets\Par ents\Paren t1.xls");
xlsMap.put("Child1","C:\us er\documen ts\Spreads heets\Pare nts\Depend ents\Child ren\Child1 .xls");
xlsMap.put("Child2","C:\us er\documen ts\Spreads heets\Pare nts\Depend ents\Child ren\Child2 .xls");
xlsMap.put("Dog1","C:\user \documents \Spreadshe ets\Depend ents\Other \Dog1.xls" );
xlsMap.put("Cat1","C:\user \documents \Spreadshe ets\Depend ents\Other \Cat1.xls" );
xsdMap.put("Parent1","C:\u ser\docume nts\Schema s\2017v1.0 \Parents\t able1.xsd" );
xsdMap.put("Child1","C:\us er\documen ts\Schemas \2017v1.0\ Parents\De pendencies \Child1.xs d");
xsdMap.put("Child2","C:\us er\documen ts\Schemas \2017v1.0\ Parents\De pendencies \Child2.xs d");
xsdMap.put("Dog1","C:\user \documents \Schemas\2 017v1.0\De pendencies \Other\Dog 1.xsd");
xsdMap.put("Cat1","C:\user \documents \Schemas\2 017v1.0\De pendencies \Other\Cat 1.xsd");
xsdMap.put("Cat2","C:\user \documents \Schemas\2 017v1.0\De pendencies \Other\Cat 2.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\docu ments\Sche mas\2017v1 .0\Depende ncies\Othe r\Cat2.xsd ","C:\user \documents \Spreadshe ets\Depend ents\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.
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:\u
xlsMap.put("Child1","C:\us
xlsMap.put("Child2","C:\us
xlsMap.put("Dog1","C:\user
xlsMap.put("Cat1","C:\user
xsdMap.put("Parent1","C:\u
xsdMap.put("Child1","C:\us
xsdMap.put("Child2","C:\us
xsdMap.put("Dog1","C:\user
xsdMap.put("Cat1","C:\user
xsdMap.put("Cat2","C:\user
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\docu
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 ;)
ASKER
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.key Set());
Set<String> newKeys = xsdKeys.removeAll(xlsMap.k eySet()); ==> here
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.key
Set<String> newKeys = xsdKeys.removeAll(xlsMap.k
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 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.
ASKER
Thanks again.
Something like this might get you started
Open in new window