Java:我们可以使用多HashMap来对结果集进行分组吗

标签 java sql-server

先决条件:Java 7 和 SQL Server 2016。

我正在从 SQL Server 获取存储过程返回的结果集

结果集:

RId ConId  ActNbr StageId  Qty   HoursInStage HoursPassed HourlyQty FlowedQty
--- ------ ------ ------- ------ ------------ ----------- --------- ---------
50  6814     77     1     24000      24           0           NULL     NULL
50  6814     77     2     36000      19           5           NULL     NULL
50  6814     77     3     48000      15           9           NULL     NULL
50  6814     77     4     60000      11           13          NULL     NULL
50  6814     77     6     60000      24           0           NULL     NULL
50  6855     33     1     0          24           0           NULL     NULL
50  6855     33     2     15000      19           5           NULL     NULL
50  6855     33     3     15000      15           9           NULL     NULL
50  6855     33     4     15000      11           13          NULL     NULL
50  6855     33     6     20000      24           0           NULL     NULL
50  176892   10     1     0          24           0           NULL     NULL
50  176892   10     2     0          19           5           NULL     NULL
50  176892   10     3     0          15           9           NULL     NULL
50  176892   10     4     0          11           13          NULL     NULL
50  176892   10     6     0          24           0           NULL     NULL
50  176892   47     1     0          24           0           NULL     NULL
50  176892   47     2     0          19           5           NULL     NULL
50  176892   47     3     0          15           9           NULL     NULL
50  176892   47     4     0          11           13          NULL     NULL
50  176892   47     6     0          24           0           NULL     NULL

我创建了一个 POJO 类来保存结果集。 我想创建一个 Hashmap 对象,其中 Key 作为 RIdConIdActNbr 和 Value 作为 List,其中包含给定键的所有值。

那么,我可以使用 HashMap 来做到这一点吗?我想我可以使用将其他 map 保存为值的 map 。但不确定如何将公共(public)键值项制作为列表。

Java 类:HoursQty

public class BurnProfileHourlyNomData {
    private Integer stageId;
    private Integer qty;
    private Integer hoursInStage;
    private Integer hoursPassed;
    private Integer hourlyQty;
    private Integer FlowedQty;
}

逻辑:

Map<Integer, Map<Integer, Map<Integer, HoursQty>>> hourlyInfo = new HashMap<>();
for(DatabaseRecord row : rows) {
    Integer rId = (Integer)row.get("RId");
    Integer conId = (Integer)row.get("ConId");
    Integer actNbr = (Integer)row.get("ActNbr");
    Integer stageId = (Integer)row.get("StageId");
    Integer qty = (Integer)row.get("Qty");
    Integer hoursInStage = (Integer)row.get("HoursInStage");
    Integer hoursPassed = (Integer)row.get("HoursPassed");
    Integer hourlyQty = (Integer)row.get("HourlyQty");
    Integer flowedQty = (Integer)row.get("FlowedQty");

    HoursQty impl = new HoursQty();
    impl.setRId(rId);
    /* Performed all set methods to assign values */
    .....
    Map<Integer, HoursQty> actNbrMap = new HashMap<Integer, HoursQty>();
    actNbrMap.put(actNbr, impl);

    Map<Integer, Map<Integer, HoursQty>> conIdMap = new HashMap<Integer, Map<Integer, HoursQty>>();
    conIdMap.put(conId, actNbrMap);

    hourlyInfo.put(rId, conIdMap);
}

尝试使用 entryMap 键和值读取此内容,但得到的是单独的行值。 不确定如何根据 rIdconIdactNbr 作为键对所有列表进行分组,并检索与列表具有相同键的所有行。

最佳答案

你必须像这样设计你的类。

类 AccountKey 将用作 HashMap 中的键。代码如下。

public class AccountKey {
  private Integer rId;
  private Integer conId;
  private Integer actNbr;

  public Integer getrId() {
    return rId;
  }

  public void setrId(Integer rId) {
    this.rId = rId;
  }

  public Integer getConId() {
    return conId;
  }

  public void setConId(Integer conId) {
    this.conId = conId;
  }

  public Integer getActNbr() {
    return actNbr;
  }

  public void setActNbr(Integer actNbr) {
    this.actNbr = actNbr;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    AccountKey that = (AccountKey) o;

    if (rId != null ? !rId.equals(that.rId) : that.rId != null) return false;
    if (conId != null ? !conId.equals(that.conId) : that.conId != null) return false;
    return actNbr != null ? actNbr.equals(that.actNbr) : that.actNbr == null;
  }

  @Override
  public int hashCode() {
    int result = rId != null ? rId.hashCode() : 0;
    result = 31 * result + (conId != null ? conId.hashCode() : 0);
    result = 31 * result + (actNbr != null ? actNbr.hashCode() : 0);
    return result;
  }
}

BurnProfileHourlyNomData 类将用作 HashMap 中的值。

public class BurnProfileHourlyNomData {
  private Integer stageId;
  private Integer qty;
  private Integer hoursInStage;
  private Integer hoursPassed;
  private Integer hourlyQty;
  private Integer FlowedQty;

  public Integer getStageId() {
    return stageId;
  }

  public void setStageId(Integer stageId) {
    this.stageId = stageId;
  }

  public Integer getQty() {
    return qty;
  }

  public void setQty(Integer qty) {
    this.qty = qty;
  }

  public Integer getHoursInStage() {
    return hoursInStage;
  }

  public void setHoursInStage(Integer hoursInStage) {
    this.hoursInStage = hoursInStage;
  }

  public Integer getHoursPassed() {
    return hoursPassed;
  }

  public void setHoursPassed(Integer hoursPassed) {
    this.hoursPassed = hoursPassed;
  }

  public Integer getHourlyQty() {
    return hourlyQty;
  }

  public void setHourlyQty(Integer hourlyQty) {
    this.hourlyQty = hourlyQty;
  }

  public Integer getFlowedQty() {
    return FlowedQty;
  }

  public void setFlowedQty(Integer flowedQty) {
    FlowedQty = flowedQty;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    BurnProfileHourlyNomData that = (BurnProfileHourlyNomData) o;

    if (stageId != null ? !stageId.equals(that.stageId) : that.stageId != null) return false;
    if (qty != null ? !qty.equals(that.qty) : that.qty != null) return false;
    if (hoursInStage != null ? !hoursInStage.equals(that.hoursInStage) : that.hoursInStage != null)
      return false;
    if (hoursPassed != null ? !hoursPassed.equals(that.hoursPassed) : that.hoursPassed != null)
      return false;
    if (hourlyQty != null ? !hourlyQty.equals(that.hourlyQty) : that.hourlyQty != null)
      return false;
    return FlowedQty != null ? FlowedQty.equals(that.FlowedQty) : that.FlowedQty == null;
  }

  @Override
  public int hashCode() {
    int result = stageId != null ? stageId.hashCode() : 0;
    result = 31 * result + (qty != null ? qty.hashCode() : 0);
    result = 31 * result + (hoursInStage != null ? hoursInStage.hashCode() : 0);
    result = 31 * result + (hoursPassed != null ? hoursPassed.hashCode() : 0);
    result = 31 * result + (hourlyQty != null ? hourlyQty.hashCode() : 0);
    result = 31 * result + (FlowedQty != null ? FlowedQty.hashCode() : 0);
    return result;
  }
}

HashMap的结构会是这样的。

Map<AccountKey,BurnProfileHourlyNomData> actBurnDataMap = new HashMap<>();

从 stroed 过程接收到数据后,填充两个对象并使用上面给出的 Map。

关于Java:我们可以使用多HashMap来对结果集进行分组吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57399051/

相关文章:

sql-server - 如何在 SQL Server 2012 中获取最后插入的 ID

c# - 如何知道在C#代码中具有TRY CATCH处理的存储过程中是否存在错误

java - 当我尝试将 JTree 添加到 JFrame 时,JTree 不更新

java - Eclipse 中的 ASTVisitor

c# - 如何增加 OrmLite ServiceStack 中的命令超时?

mysql - 如何使用 SQL 打印数据库列中的前 2 个最大值?

sql-server - 我应该如何重命名许多存储过程而不破坏内容?

Java 正则表达式 - 不能以斜杠或空格开头或结尾,并且不能连续斜杠

java - 对外部对象不需要的一些方法进行单元测试。

java - Python中通过py4j在Java corenlp情感评分程序中编译错误