Kumar
Kumar

Reputation: 961

ArrayList as a Value to HashMap returns only last element of ArrayList

Please see the below code-

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 Printing

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");
    }    
}

I am trying to use the HashMap; Key as dateTime and Value as List. However it always returns when I am iterating and printing the value.

Thanks Kumar Shorav

Upvotes: 2

Views: 1003

Answers (3)

Aaron Digulla
Aaron Digulla

Reputation: 328584

If you want to accumulate elements in a list used as value of a map, you have to use this approach:

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

 list.add( ... );

Upvotes: 0

ashokramcse
ashokramcse

Reputation: 2861

On your Getters and Setters of the PanchangaElementInformationBean class change the data type from Date to ArrayList<Date> and add that setStartTime to as bellow so you can get all values from it.

    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);
  }

So while you iterate you can get all the values from the ArrayList

And add this on the main printing for loop to print individual Date from ArrayList

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

And Initialize lstTime = new ArrayList<ElementInformationBean>(); on above the For Loop so that you can add many elements are else it will add only one for loop instance value repeatedly to the list.

Upvotes: 1

SANN3
SANN3

Reputation: 10069

Initialize lstTime outside the loop.

Try this code :

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);
    }
}

Upvotes: 2

Related Questions