gpt4 book ai didi

java - 比较Hashmap下载excel中的数据

转载 作者:行者123 更新时间:2023-12-01 19:21:06 24 4
gpt4 key购买 nike

我有这 4 个 HashMap ,并在代码中使用它们,这样我就可以在 Excel 工作表中显示比较结果。

HashMap 1 - 具有唯一 id 和值的键作为另一个包含 tagid 作为键和要比较的字段描述的 HashMap 。

[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 工作表中显示比较。

alt text

最佳答案

我首先建议您将次优数据表示标准化为如下所示。那么你只需要维护两张 map 。然后很容易在它们之间进行迭代并以您想要的方式显示。

如果你可以使用google-guava库,然后使用 Multiset 按 id 进行分组就更容易了。

以下是我的方法的高级细节。您可以使用“reportBuilder.build(plan1, plan2)”的返回类型并按照其他人的建议使用 Apache POI 来创建 Excel

  • 运营商提供 1 个或多个计划
  • 每个计划都有 ID 和 1 个或多个功能
  • 每个功能都有 id、说明、值

    公共(public)类主要{私有(private)静态 map > plan1Map;

    私有(private)静态 map > plan1AdditionalDetailsMap;

    私有(private)静态 map > plan2Map;

    私有(private)静态 map > plan2AdditionalDetailsMap;

    私有(private)静态计划 plan1;

    私有(private)静态计划 plan2;

    公共(public)静态无效主(最终字符串[]参数){ 初始化数据(); 标准化数据(); System.out.println(计划1); System.out.println(plan2); PlanComaprisionReportBuilder reportBuilder = new PlanComaprisionReportBuilder(); System.out.println(reportBuilder.build(计划1, 计划2));}

    私有(private)静态无效正常化数据(){ 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();


    <pre><code>String tagId = entry.getKey();
    String tagDescription = entry.getValue();
    String tagValue = additionalDetails.get(tagId);


    plan.addFeature(new Feature(tagId, tagDescription, tagValue));
    </code></pre>

    }
    return plan;

    }

    私有(private)静态无效initiaizeData(){ plan1Map = Maps.newHashMap(); map 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);

    }}

公开课计划{ 私有(private)最终字符串 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;
}
}

关于java - 比较Hashmap下载excel中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4206951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com