List list = new ArrayList();   
list.add(new HashMap().put("1", "2")); //1 
  
Map map = new HashMap();   
map.put("1", "2");   
list.add(map);  //2
  
for (Iterator iterator = list.iterator(); iterator.hasNext();) {   
    Map map1 = (Map) iterator.next();   
    System.out.println(map1);   
}  结果如下: 
null 
{1=2} 为何不同呢? 

解决方案 »

  1.   

    list.add(new HashMap().put("1", "2"));这句话出问题了。你使用new HashMap().put("1", "2")返回的是put的返回值,而不是map对象。由于put会返回之前的关键字的值,同时之前map中没有1这个关键字,所以返回null。这里你就相当于执行了list.add(null);
      

  2.   

    执行速度问题
    list.add(new HashMap().put("1", "2"));
    list较hashMap快;
    所以在没填充完HashMap list已经执行完毕