List边循环,边删除;这种一听感觉就像是会出问题一样,其实只要是删除特定数据,就不会出问题,你如果直接循环删除所有数据,那可能就会出问题了,比如:
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
for (int i=0;i<list.size();i++){
list.remove(i);
}
System.out.println(list);
}
结果:
[2, 4]
删除的所有,但是最终还留下两个值,这是因为当一个元素被移除时,该List的大小(size)就会缩减,同时也改变了索引的指向,也就是上面的代码只会循环两次,长度在不断减少,第一次循环0 < 4 ,第二次循环 1 < 3 ,不满足下一次循环条件 2 < 2,故只有两次循环就结束。所以,在迭代的过程中使用索引,将无法从List中正确地删除多个指定的元素。
当使用了foreach 如下:
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
for (String i:list){
list.remove(i);
}
System.out.println(list);
}
结果直接报错:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at com.gwh.demo.test.ListTest.main(ListTest.java:18)
这是因为,在 foreach循环中,编译器使得 remove()方法先于next()方法被调用,因为先执行了remove()方法,导致next()获取的数组长度和remove()后的数组长度不一致,则抛出异常。
如果使用了Iterator 迭代器模式,如下:
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
// 必须先执行一下next() 否则会抛出异常
iterator.next();
iterator.remove();
}
System.out.println(list);
}
这样保证next()先执行一下,这样删除所有就不会出问题了。
如果是循环List,同时删除符合要求的数据,则不管使用那种方式都不会出现问题。demo代码如下:
for循环:
public static void main(String[] args) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("id",1);
map.put("name","张三");
map.put("sex","男");
map.put("age",18);
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("id",2);
map1.put("name","李四");
map1.put("sex","男");
map1.put("age",18);
HashMap<String, Object> map2 = new HashMap<String, Object>();
map2.put("id",3);
map2.put("name","王五");
map2.put("sex","男");
map2.put("age",18);
HashMap<String, Object> map3 = new HashMap<String, Object>();
map3.put("id",4);
map3.put("name","赵六");
map3.put("sex","男");
map3.put("age",18);
List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
list.add(map);
list.add(map1);
list.add(map2);
list.add(map3);
for(int i=0;i<list.size();i++){
if(Integer.valueOf(String.valueOf(list.get(i).get("id")))== 1){
list.remove(i);
}
}
System.out.println(list);
}
结果如下:
[{sex=男, name=李四, id=2, age=18}, {sex=男, name=王五, id=3, age=18}, {sex=男, name=赵六, id=4, age=18}]
迭代器如下:
public static void main(String[] args) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("id",1);
map.put("name","张三");
map.put("sex","男");
map.put("age",18);
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("id",2);
map1.put("name","李四");
map1.put("sex","男");
map1.put("age",18);
HashMap<String, Object> map2 = new HashMap<String, Object>();
map2.put("id",3);
map2.put("name","王五");
map2.put("sex","男");
map2.put("age",18);
HashMap<String, Object> map3 = new HashMap<String, Object>();
map3.put("id",4);
map3.put("name","赵六");
map3.put("sex","男");
map3.put("age",18);
List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
list.add(map);
list.add(map1);
list.add(map2);
list.add(map3);
Iterator<HashMap<String, Object>> iterator = list.iterator();
while (iterator.hasNext()){
HashMap<String, Object> next = iterator.next();
if(Integer.valueOf(String.valueOf(next.get("id")))== 1){
iterator.remove();
}
}
System.out.println(list);
}
结果:
[{sex=男, name=李四, id=2, age=18}, {sex=男, name=王五, id=3, age=18}, {sex=男, name=赵六, id=4, age=18}]
还可以使用 JDK8特性stream 流过滤:
public static void main(String[] args) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("id",1);
map.put("name","张三");
map.put("sex","男");
map.put("age",18);
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("id",2);
map1.put("name","李四");
map1.put("sex","男");
map1.put("age",18);
HashMap<String, Object> map2 = new HashMap<String, Object>();
map2.put("id",3);
map2.put("name","王五");
map2.put("sex","男");
map2.put("age",18);
HashMap<String, Object> map3 = new HashMap<String, Object>();
map3.put("id",4);
map3.put("name","赵六");
map3.put("sex","男");
map3.put("age",18);
List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
list.add(map);
list.add(map1);
list.add(map2);
list.add(map3);
List<HashMap<String, Object>> co = list.stream()
.filter(p -> Integer.valueOf(String.valueOf(p.get("id"))) != 1)
.collect(Collectors.toList());
System.out.println(co);
}
结果:
[{sex=男, name=李四, id=2, age=18}, {sex=男, name=王五, id=3, age=18}, {sex=男, name=赵六, id=4, age=18}]