簡體   English   中英

獲取嵌套HashMap的值

[英]Getting values for nested HashMap

我在地圖中有地圖和列表,並且想從數據庫獲取該地圖對象的數據,有人可以建議我以一種正確的方式在此代碼上獲取兩個地圖的鍵和值嗎,我有一個對象A它包含一個具有C值列表的對象B,我正在獲取A對象的數據,但是B和C對象的數據在所有A對象中都是相似的,並且C對象僅是一點,而沒有呈現所有值,其中我錯了嗎?:

void getData(int dataNo) {

AsyncCallback<Map<A, Map<B,List<C>>>> callback = new AsyncCallback<Map<A, Map<B,List<C>>>>() {

  public void onFailure(Throwable caught) {

  }

  public void onSuccess(Map<A, Map<B,List<C>>> result) {

    panel.clear();

    for (Map.Entry<A, Map<B,List<C>>> entry : result.entrySet()) {
      A a = entry.getKey();
      Map<B, List<C>> bc = entry.getValue();

      chart = new Chart().setType(Series.Type.LINE)
                  .setLegend(new Legend()
                                 .setLayout(Legend.Layout.VERTICAL)
                                 .setAlign(Legend.Align.RIGHT)
                                 .setVerticalAlign(Legend.VerticalAlign.TOP)
                                 .setX(-10).setY(100)
                                 .setBorderWidth(0))
                                 .setZoomType(Chart.ZoomType.X_AND_Y);

      chart.setChartTitleText(a.getL());

      for(Map.Entry<B, List<C>> in : bc.entrySet()){
        B b = in.getKey();
        List<C> c=in.getValue();

        bc.containsKey(b);
        bc.containsValue(c);
        serie = chart.createSeries().setName(b.getF());
        C[] ac=c.toArray(new C[c.size()]);
        for (C cd: ac) {
          serie.setPoints(new Number[] { cd.getS()});
        }

        chart.addSeries(serie);

      }
      panel.add(chart);
    }

  }

};  

編輯填充地圖:

   public Map<A, Map<B, List<C>>> getL(int dataNo) {
    Map<A, Map<B, List<C>>> outermap = new HashMap<A, Map<B, List<C>>>();
    Map<B,List<C>> innermap=new HashMap<B,List<C>>();

    List<A> a= DB.getL(dataNo);
    for (A aa: a) {
        outermap.put(aa, innermap);
        List<B> b=DB.getK(aa.getId());
        for(B bb: b){
        innermap.put(bb, DB.getD(bb.getId()));
     }

    }
    return outermap;
}

當您填充outermap您需要為每個條目使用不同的innermap 您的代碼應為:

public Map<A, Map<B, List<C>>> getL(int dataNo) {
  Map<A, Map<B, List<C>>> outermap = new HashMap<A, Map<B, List<C>>>();
  List<A> a= DB.getL(dataNo);
  for (A aa: a) {
    Map<B,List<C>> innermap=new HashMap<B,List<C>>();
    outermap.put(aa, innermap);
    List<B> b=DB.getK(aa.getId());
    for(B bb: b){
      innermap.put(bb, DB.getD(bb.getId()));
    }
  }
  return outermap;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM