I have these 4 hashmaps and use them in code so I can show the comparison in an excel sheet.
HashMap 1 - is with a key of unique id and value as another hashmap containing tagid as key and description of fields to compare.
[343, ((id_1,Plan Features),(a, Deductible),(b,Individual),(c,Family),(id_4,Individual Out-of-network),(id_2, Out-of-pocket Annual Maximum),(d,Individual),(e,Family),(u, Life Time Maximum))]
HashMap 2 - is w开发者_Go百科ith a key of unique id same as Hashmap 1 and value as another hashmap containing tagid as key and value of description used in Hashmap 1.
[343, ((id_1,""),(a, Calendar Year),(b,5000),(c,10000)(id_4,15000),(id_2,""),(d,5000),(e,10000),(u,"Unlimited"))]
Same is the case with HashMap 3 and HashMap 4
[347, ((id_1,Plan Features),(a, Deductible),(b,Individual),(id_5, Individual Out-of-network),(c,Family),(id_4,Family Out-of-network),(id_2, Out-of-pocket Annual Maximum),(d,Individual),(e,Family),(u, Life Time Maximum))]
[347, ((id_1,""),(a, Calendar Year),(b,7000),(id_5, 9000),(c,12000),(id_4,14000),(id_2, ""),(d,6000),(e,15000),(u, "Unlimited"))]
I want to show the comparison in an excel sheet by showing all descriptions in one column and respective values in another 2 columns.
I'd first suggest you to normalize your suboptimal data representation to something like below. Then you just need to maintain TWO Maps. Then is is easy to iterate between them and display whichever way you want.
If you can use google-guava library then it is even easier to group then by id using Multiset.
Below are highlevel details on my approach. You can use the return type of "reportBuilder.build(plan1, plan2)" and use Apache POI as suggested by others to create the excel
- A Carrier offers 1 or more Plan's
- Each Plan has and id and 1 or more Features
Each Feature has id, decsription, value
public class Main { private static Map> plan1Map;
private static Map> plan1AdditionalDetailsMap;
private static Map> plan2Map;
private static Map> plan2AdditionalDetailsMap;
private static Plan plan1;
private static Plan plan2;
public static void main(final String[] args) { initiaizeData(); normalizeData(); System.out.println(plan1); System.out.println(plan2); PlanComaprisionReportBuilder reportBuilder = new PlanComaprisionReportBuilder(); System.out.println(reportBuilder.build(plan1, plan2)); }
private static void normalizeData() { plan1 = buildPlan(plan1Map, plan1AdditionalDetailsMap);
plan2 = buildPlan(plan2Map, plan2AdditionalDetailsMap);
}
private static Plan buildPlan(final Map<String, Map<String, String>> planMap, final Map<String, Map<String, String>> planAdditionalDetailsMap)
{ String planId = Iterables.getOnlyElement(planMap.keySet());
Plan plan = new Plan(planId); Map<String, String> planDetails = planMap.get(planId); Iterator<Entry<String, String>> features = planDetails.entrySet().iterator(); Map<String, String> additionalDetails = planAdditionalDetailsMap.get(planId); while (features.hasNext()) { Entry<String, String> entry = features.next();
} return plan;String tagId = entry.getKey(); String tagDescription = entry.getValue(); String tagValue = additionalDetails.get(tagId); plan.addFeature(new Feature(tagId, tagDescription, tagValue));
}
private static void initiaizeData() { plan1Map = Maps.newHashMap(); Map map1Value = Maps.newTreeMap(); map1Value.put("id_1", "Plan Features"); map1Value.put("a", "Deductible"); map1Value.put("b", "Individual"); map1Value.put("c", "Family"); map1Value.put("id_4", "Individual Out-of-network"); map1Value.put("id_2", "Out-of-pocket Annual Maximum"); map1Value.put("d", "Individual"); map1Value.put("e", "Family"); map1Value.put("u", "Life Time Maximum"); plan1Map.put("343", map1Value);
plan1AdditionalDetailsMap = Maps.newHashMap(); Map<String, String> policy1ExtensionValue = Maps.newTreeMap(); policy1ExtensionValue.put("id_1", ""); policy1ExtensionValue.put("a", "Calendar Year"); policy1ExtensionValue.put("b", "5000"); policy1ExtensionValue.put("c", "10000"); policy1ExtensionValue.put("id_4", "15000"); policy1ExtensionValue.put("id_2", ""); policy1ExtensionValue.put("d", "5000"); policy1ExtensionValue.put("e", "10000"); policy1ExtensionValue.put("u", "Unlimited"); plan1AdditionalDetailsMap.put("343", policy1ExtensionValue); plan2Map = Maps.newHashMap(); Map<String, String> policy2Value = Maps.newTreeMap(); policy2Value.put("id_1", "Plan Features"); policy2Value.put("a", "Deductible"); policy2Value.put("b", "Individual"); policy2Value.put("id_5", "Individual Out-of-network"); policy2Value.put("c", "Family"); policy2Value.put("id_4", "Family Out-of-network"); policy2Value.put("id_2", "Out-of-pocket Annual Maximum"); policy2Value.put("d", "Individual"); policy2Value.put("e", "Family"); policy2Value.put("u", "Life Time Maximum"); plan2Map.put("347", policy2Value); plan2AdditionalDetailsMap = Maps.newHashMap(); Map<String, String> policy2ExtensionValue = Maps.newTreeMap(); policy2ExtensionValue.put("id_1", ""); policy2ExtensionValue.put("a", "Calendar Year"); policy2ExtensionValue.put("b", "7000"); policy2ExtensionValue.put("id_5", "9000"); policy2ExtensionValue.put("c", "12000"); policy2ExtensionValue.put("id_4", "14000"); policy2ExtensionValue.put("id_2", ""); policy2ExtensionValue.put("d", "6000"); policy2ExtensionValue.put("e", "15000"); policy2ExtensionValue.put("u", "Unlimited"); plan2AdditionalDetailsMap.put("347", policy2ExtensionValue);
} }
public class Plan { private final String id;
private final Set<Feature> features = Sets.newHashSet();
public Plan(final String id)
{
this.id = id;
}
public String getId()
{
return id;
}
public void addFeature(final Feature f)
{
features.add(f);
}
public Set<Feature> getFeatures()
{
return Collections.unmodifiableSet(features);
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
Plan other = (Plan) obj;
if (id == null)
{
if (other.id != null)
{
return false;
}
}
else if (!id.equals(other.id))
{
return false;
}
return true;
}
@Override
public String toString()
{
return "Plan [features=" + features + ", id=" + id + "]";
}
}
public class Feature
{
private final String id;
private final String description;
private final String value;
public Feature(final String id, final String description, final String value)
{
this.id = id;
this.description = description;
this.value = value;
}
public String getId()
{
return id;
}
public String getDescription()
{
return description;
}
public String getValue()
{
return value;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
Feature other = (Feature) obj;
if (id == null)
{
if (other.id != null)
{
return false;
}
}
else if (!id.equals(other.id))
{
return false;
}
return true;
}
@Override
public String toString()
{
return "Attribute [description=" + description + ", id=" + id + ", value=" + value + "]";
}
}
public class PlanComaprisionReportBuilder
{
Multimap<String, String> build(final Plan... plans)
{
Multimap<String, String> rows = ArrayListMultimap.create(100, plans.length);
for (Plan p : plans)
{
for (Feature f : p.getFeatures())
{
rows.put(f.getDescription(), f.getValue() != null ? f.getValue() : "");
// if (!rows.containsKey(f.getDescription()))
// {
// }
// else
// {
// existing row needs separate handling
// }
}
}
return rows;
}
}
So you have two sets of hashmaps you want to compare in an excel file and both sets might or might not have the same informations in it... (and lets assume that if they have the same information, they might or not have the same keys in the maps)
I'm not sure I get what is the problem you're stuck on exactly, but here is how I would quickly go about this.
I would have a small class holding the row label ("Plan Features", for example), and holding the values of both maps you want to compare (valA, valB, for example), so something like this:
class ThisIsARow {
String label, valA, valB;
}
I would combine both sets of hashmaps in a resulting one HashMap<String, ThisIsARow>
for which the key would be the label itself.
I would then loop through the first set of hashmaps, creating new instances of ThisIsARow, setting their label and valA values for each.
Then I would loop through the second set of hashmaps, looking first if there is already a ThisIsARow instance in the resulting HashMap<String, ThisIsARow>
for each label, creating and adding a new one (setting its label and valB) if there's none yet for this label, or else just set the valB of the existing ThisIsARow instance.
Then I'd use Apache POI to write everything down in an Excel file. (You then only have to loop through the resulting hashmap, printing one instance of ThisIsARow per row.)
label valA valB
label valA valB
label valA valB
label valA valB
...
I hope this helps. Let me know if you need clarifications or if I'm offtrack!
精彩评论