Javaの:私たちはグループに結果セットをマルチHashMapを使用することができます

ラジK:

前提条件:Javaの7とSQL Server 2016。

私は、SQL Serverからのストアドプロシージャによって返される結果セットを取得しています

ResultSetの:

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クラスを作成しました。私はキーとして持つハッシュマップオブジェクトを作成したいRIdConIdActNbr指定されたキーのすべての値を保持し、リストとして値を。

だから、私はHashMapを使用していることを行うことができますか?私はバリューなどの他の地図を保持している地図を使用することができます思いました。しかし、いないいないリストとして共通のキーと値の項目を作成する方法を確認してください。

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のキーと値を読み取ることを試みたが、個々の行の値を取得します。わからないグループにどのように基づいて、すべてのリストrIdconIdおよびactNbrキーなど、リストと同じキーを持つすべての行を取得します。

sambit:

あなたはこのようなあなたのクラスを設計する必要があります。

HashMapの中でキーとして使用されるクラスAccountKey。コードは以下のとおりです。

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プロシージャからのデータを受信すると、両方のオブジェクトを移入し、地図上に与えられます。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=235978&siteId=1