java.util.ConcurrentModificationException产生原因及解决办法
第一次遇到此异常是在处理3g.renren.com的好友分组功能中,因为接口提供的好友分组(以map的方式提供好友分组的id跟分组名)中没有把分组名为空,但是id存在的数据屏蔽掉,所以我在调用接口服务之后,需要重新处理value为空的数据。
代码如下:
view plain
/** * * Dec 1, 2011 * @author 车前猛跑 */ public class MapDemo { public static void main (String [] args) { Map<Integer, String> map = new HashMap<Integer, String>(); map.put(5, null); //① map.put(1, "a"); //② map.put(3, null); //③ MapDemo md = new MapDemo(); Map<Integer , String> newmap = md.clearMap(map); // 通过新map返回数据 for (Map.Entry<Integer, String> entry : newmap.entrySet()) { System.out.println(entry.getKey()); } if (map.isEmpty()) { System.out.println("empty"); } } private Map<Integer , String> clearMap (Map<Integer , String> map) { Map<Integer , String> retmap = new HashMap<Integer , String>(); for (Map.Entry<Integer, String> entry : map.entrySet()) { if (entry.getValue() != null) { retmap.put(entry.getKey(), entry.getValue()); } } return retmap; // 通过新map返回数据 } }