【发布时间】:2014-04-28 17:06:37
【问题描述】:
请看下面的代码-
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;作为日期时间的键和作为列表的值。但是,当我迭代和打印值时它总是返回。
谢谢 库马尔·肖拉夫
【问题讨论】:
-
它返回什么?由于您的问题无法完全理解..
-
它只返回List中的一个元素,即列表的最后一个值;它在添加到地图时显示列表大小为 2。但是在打印时它只打印一个。