簡體   English   中英

ArrayList作為HashMap的值僅返回ArrayList的最后一個元素

[英]ArrayList as a Value to HashMap returns only last element of ArrayList

請參閱以下代碼-

Map<DateTime, ArrayList<ElementInformationBean>> mapTithi = new HashMap<DateTime, ArrayList<ElementInformationBean>>();
List<ElementInformationBean> lstTime = null;
ElementInformationBean curntTithi = null;
ElementInformationBean nextTithi = null;
for (int i = 0; i < lstNakstra.size()-1; i++) {
    lstTime = new ArrayList<ElementInformationBean>();
    curntTithi = lstNakstra.get(i);
    nextTithi = lstNakstra.get(i+1);
    if(curntTithi.getStartTime().toDateMidnight().equals(nextTithi.getStartTime().toDateMidnight()))
        {
        lstTime.add(curntTithi);
        lstTime.add(nextTithi);
        mapTithi.put(curntTithi.getStartTime().toDateMidnight().toDateTime(), (ArrayList<ElementInformationBean>) lstTime);
    } else {
        lstTime.add(curntTithi);
                mapTithi.put(curntTithi.getStartTime().toDateMidnight().toDateTime(), (ArrayList<ElementInformationBean>) lstTime);
    }
}

印刷用

for (Map.Entry<DateTime, ArrayList<PanchangaElementInformationBean>> entry : mapTithi.entrySet()) {
    DateTime key = entry.getKey();
    ArrayList<PanchangaElementInformationBean> values = entry.getValue();
    System.out.println("Key = " + key);
    for (PanchangaElementInformationBean p: values) {
        System.out.print("Values = " + p.getStartTime() + "n");
    }    
}

我正在嘗試使用HashMap; 鍵為dateTime,值為List。 但是,當我迭代並打印該值時,它總是返回。

感謝Kumar Shorav

在循環外初始化lstTime

試試這個代碼:

Map<DateTime, ArrayList<ElementInformationBean>> mapTithi = new HashMap<DateTime, ArrayList<ElementInformationBean>>();

// Intialize here
List<ElementInformationBean> lstTime = new ArrayList<ElementInformationBean>(); 

ElementInformationBean curntTithi = null;
ElementInformationBean nextTithi = null;
for (int i = 0; i < lstNakstra.size()-1; i++) {
    curntTithi = lstNakstra.get(i);
    nextTithi = lstNakstra.get(i+1);
    if(curntTithi.getStartTime().toDateMidnight().equals(nextTithi.getStartTime().toDateMidnight()))
        {
        lstTime.add(curntTithi);
        lstTime.add(nextTithi);
        mapTithi.put(curntTithi.getStartTime().toDateMidnight().toDateTime(), (ArrayList<ElementInformationBean>) lstTime);
    } else {
        lstTime.add(curntTithi);
                mapTithi.put(curntTithi.getStartTime().toDateMidnight().toDateTime(), (ArrayList<ElementInformationBean>) lstTime);
    }
}

PanchangaElementInformationBean類的GettersSetters上,將數據類型從Date更改為ArrayList<Date> ,並將該setStartTime添加到下面,以便可以從中獲取所有值。

    private ArrayList<Date> startTime;

    public ArrayList<Date> getStartTime() {
         return startTime;
    }

    public void setStartTime(Date item) {
        if (startTime == null) {
            startTime = new ArrayList<Date>();
        }
     this.startTime.add(item);
  }

因此,當您迭代時,您可以從ArrayList獲取所有值

並將其添加到主打印for loop以從ArrayList打印單個Date

    for (Date startTime : p.getStartTime()) {
        System.out.println(startTime);
    }

並初始化lstTime = new ArrayList<ElementInformationBean>(); 在For循環上方,以便您可以添加許多元素,否則它將僅向循環添加一個for循環實例值。

如果要在列表中累積用作地圖值的元素,則必須使用以下方法:

 List<...> list = map.get( key );
 if( null == list ) {
     list = new ...;
     map.put( key, list );
 }

 list.add( ... );

暫無
暫無

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

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