连续remove,第二个remove的已经被第一个remove掉了
加continue可解决
HashMap h1 = new HashMap();
h1.put("userId", "1");
HashMap h2 = new HashMap();
h2.put("userId", "2");
List<HashMap> hashMapList = new ArrayList<>();
hashMapList.add(h1);
hashMapList.add(h2);
System.out.println("hashMapList:"+hashMapList);
List<Integer> memberIdList = new ArrayList<>();
memberIdList.add(new Integer(1));
memberIdList.add(new Integer(3));
System.out.println("memberIdList:"+memberIdList);
Set<Integer> memberSet =new HashSet(memberIdList);
Iterator<HashMap> hashMapOldIterator = hashMapList.iterator();
while (hashMapOldIterator.hasNext()) {
HashMap h = hashMapOldIterator.next();
System.out.println("aaaa:"+h.entrySet());
if (memberSet.contains( Integer.parseInt(h.get("userId").toString()))){
hashMapOldIterator.remove();
continue;
}
if (h.get("userId").toString().equals("1")){
System.out.println("bbb:"+hashMapList);
hashMapOldIterator.remove();
continue;
}
}
System.out.println("ccc:"+hashMapList);
最后
以上就是无奈太阳最近收集整理的关于用迭代器多次.remove()时报错 Java.lang.IllegalStateException的原因的全部内容,更多相关用迭代器多次.remove()时报错内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复