我有这4个哈希图并在代码中使用它们,所以我可以在excel表中显示比较。
HashMap 1 - 使用唯一id和value的键作为另一个hashmap,其中包含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中使用的键和描述值的hashmap。
[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"))]
我想在excel表中显示比较,方法是在一列中显示所有描述,在另外两列中显示相应的值。
答案 0 :(得分:2)
我首先建议您将次优数据表示规范化为如下所示。然后你只需要维护两张地图。然后很容易在它们之间进行迭代并显示你想要的任何方式。
如果你可以使用google-guava库,那么使用Multiset进行分组就更容易了。
以下是我的方法的高级细节。您可以使用返回类型“reportBuilder.build(plan1,plan2)”并按照其他人的建议使用Apache POI来创建excel
每个功能都有id,decsription,value
公共课主 { private static Map> plan1Map;
私人静态地图> plan1AdditionalDetailsMap;
私人静态地图> plan2Map;
私人静态地图> plan2AdditionalDetailsMap;
private static Plan plan1;
private static Plan plan2;
public static void main(final String [] args) { initiaizeData(); normalizeData(); 的System.out.println(计划1); 的System.out.println(计划2); 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();
String tagId = entry.getKey();
String tagDescription = entry.getValue();
String tagValue = additionalDetails.get(tagId);
plan.addFeature(new Feature(tagId, tagDescription, tagValue));
}
return plan;
}
private static void initiaizeData() { plan1Map = Maps.newHashMap(); Map map1Value = Maps.newTreeMap(); map1Value.put(“id_1”,“计划功能”); map1Value.put(“a”,“Deductible”); map1Value.put(“b”,“Individual”); map1Value.put(“c”,“Family”); map1Value.put(“id_4”,“Individual out-of-network”); map1Value.put(“id_2”,“自掏腰包年度最大值”); map1Value.put(“d”,“Individual”); map1Value.put(“e”,“Family”); map1Value.put(“u”,“Life Time Maximum”); plan1Map.put(“343”,map1Value);
String tagId = entry.getKey();
String tagDescription = entry.getValue();
String tagValue = additionalDetails.get(tagId);
plan.addFeature(new Feature(tagId, tagDescription, tagValue));
} }
公共课计划 { private final String id;
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);
}
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 + "]";
}
答案 1 :(得分:2)
因此,您要在excel文件中有两组要比较的哈希映射,并且两个集合中可能有也可能没有相同的信息......(并假设如果它们具有相同的信息,它们可能与否在地图中使用相同的键)
我不确定自己到底遇到了什么问题,但这就是我如何快速解决这个问题。
我会有一个小类拿着行标签(例如“Plan Features”),并保存你想要比较的两个地图的值(例如valA,valB),所以像这样:
class ThisIsARow {
String label, valA, valB;
}
我会将两组哈希图组合在一个HashMap<String, ThisIsARow>
中,其中键将是标签本身。
然后我会遍历第一组哈希映射,创建ThisIsARow的新实例,为每个哈希映射设置标签和valA值。
然后我将循环遍历第二组哈希映射,首先查看每个标签的结果HashMap<String, ThisIsARow>
中是否已存在ThisIsARow实例,创建并添加新的(设置其标签和valB)如果有的话还没有这个标签,或者只是设置现有ThisIsARow实例的valB。
然后我会使用Apache POI在Excel文件中写下所有内容。 (然后,您只需遍历生成的hashmap,每行打印一个ThisIsARow实例。)
label valA valB
label valA valB
label valA valB
label valA valB
...
我希望这会有所帮助。如果您需要澄清或者我不在场,请告诉我们。