我有这4个哈希图,并在代码中使用它们,因此我可以在Excel工作表中显示比较结果。
HashMap 1-具有唯一ID和值的键,是另一个包含tagid作为键和要比较的字段描述的哈希图。
[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-具有与Hashmap 1相同的唯一ID的键,并且值是另一个包含tagid作为键和Hashmap 1中使用的描述值的哈希图的值。
[343, ((id_1,""),(a, Calendar Year),(b,5000),(c,10000)(id_4,15000),(id_2,""),(d,5000),(e,10000),(u,"Unlimited"))]
HashMap 3和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"))]
我想通过在一个栏中显示所有描述,并在另外2栏中显示各个值,来在excel工作表中显示比较。
最佳答案
我首先建议您将次优数据表示形式标准化为如下所示。然后,您只需要维护两个地图。然后很容易在它们之间进行迭代并显示您想要的任何方式。
如果可以使用google-guava库,则使用Multiset按ID进行分组甚至更加容易。
以下是有关我的方法的高级详细信息。您可以使用返回类型“ reportBuilder.build(plan1,plan2)”,并按照其他人的建议使用Apache POI创建excel。
承运人提供1个或多个计划的
每个计划都有ID和1个或多个功能
每个功能都有ID,说明,值
公共班级
{
私有静态Map> plan1Map;
私人静态地图> plan1AdditionalDetailsMap;
私有静态Map> plan2Map;
私人静态地图> plan2AdditionalDetailsMap;
私人静态计划计划1;
私人静态计划计划2;
公共静态void主(最终String [] args)
{
initiaizeData();
normalizeData();
System.out.println(plan1);
System.out.println(plan2);
PlanComaprisionReportBuilder reportBuilder = new PlanComaprisionReportBuilder();
System.out.println(reportBuilder.build(plan1,plan2));
}
私有静态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)
{
字符串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();
String tagId = entry.getKey();
String tagDescription = entry.getValue();
String tagValue = additionalDetails.get(tagId);
plan.addFeature(new Feature(tagId, tagDescription, tagValue));
}
退货计划;
}
私有静态无效initiaizeData()
{
plan1Map = Maps.newHashMap();
地图map1Value = Maps.newTreeMap();
map1Value.put(“ id_1”,“计划功能”);
map1Value.put(“ a”,“免赔额”);
map1Value.put(“ b”,“个体”);
map1Value.put(“ c”,“家庭”);
map1Value.put(“ id_4”,“单个网络外”);
map1Value.put(“ id_2”,“自付费用的年度最高限额”);
map1Value.put(“ d”,“个人”);
map1Value.put(“ e”,“家庭”);
map1Value.put(“ u”,“最长生存时间”);
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);
}
}
公共课程计划
{
私有最终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;
}
}