Java ArrayList作为HashMap的值只返回ArrayList的最后一个元素
请参阅下面的代码-Java ArrayList作为HashMap的值只返回ArrayList的最后一个元素,java,iterator,hashmap,Java,Iterator,Hashmap,请参阅下面的代码- Map<DateTime, ArrayList<ElementInformationBean>> mapTithi = new HashMap<DateTime, ArrayList<ElementInformationBean>>(); List<ElementInformationBean> lstTime = null; ElementInformationBean curntTithi = null; Ele
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);
}
}
Map-mapthi=new HashMap();
列表lstime=null;
ElementInformationBean curntTithi=null;
ElementInformationBean nextIthi=null;
对于(int i=0;i
印刷用
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");
}
}
for(Map.Entry:mapthi.entrySet()){
DateTime key=entry.getKey();
ArrayList values=entry.getValue();
System.out.println(“Key=“+Key”);
for(PanchangaElementInformationBean p:values){
System.out.print(“Values=“+p.getStartTime()+”n”);
}
}
我正在尝试使用HashMap;键为dateTime,值为List。但是,当我迭代和打印值时,它总是返回
谢谢
Kumar Shorav在
PanchangaelementInformation Bean
类的getter
和setter
上,将数据类型从日期更改为数组列表
,并将设置开始时间
添加到如下所示,以便您可以从中获取所有值
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);
}
并初始化lstime=newarraylist()代码>位于For循环上方,以便您可以添加多个元素,否则它将仅向列表中重复添加一个For循环实例值。初始化lstime
请尝试以下代码:
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);
}
}
Map-mapthi=new HashMap();
//在这里初始化
List lstime=new ArrayList();
ElementInformationBean curntTithi=null;
ElementInformationBean nextIthi=null;
对于(int i=0;i
如果要在用作映射值的列表中累积元素,必须使用以下方法:
List<...> list = map.get( key );
if( null == list ) {
list = new ...;
map.put( key, list );
}
list.add( ... );
List List=map.get(键);
if(null==列表){
列表=新。。。;
地图。放置(键、列表);
}
添加(…);
它返回什么?由于您的问题不完全可理解..它只返回列表中的一个元素,即列表的最后一个值;添加到地图时,它显示列表大小为2。但当打印时,它只是打印一个。这样,我添加了所有元素。对于每个键,将添加一个新的arraylist。这就是我在循环中初始化它的原因。谢谢
List<...> list = map.get( key );
if( null == list ) {
list = new ...;
map.put( key, list );
}
list.add( ... );