【问题标题】:ArrayList as a Value to HashMap returns only last element of ArrayListArrayList 作为 HashMap 的值仅返回 ArrayList 的最后一个元素
【发布时间】: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。但是在打印时它只打印一个。

标签: java iterator hashmap


【解决方案1】:

在循环外初始化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);
    }
}

【讨论】:

  • 这样,我添加了所有元素。对于每个键,将添加一个新的数组列表。这就是我在循环内初始化它的原因。谢谢
【解决方案2】:

PanchangaElementInformationBean 类的GettersSetters 上,将数据类型从Date 更改为ArrayList&lt;Date&gt;,并将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);
    }

并在 For 循环上方初始化 lstTime = new ArrayList&lt;ElementInformationBean&gt;();,以便您可以添加许多元素,否则它只会将一个 for 循环实例值重复添加到列表中。

【讨论】:

    【解决方案3】:

    如果你想在一个列表中累积元素用作地图的值,你必须使用这种方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2016-07-21
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多