Java中List遍历删除元素remove()的方法

2022-07-27,,,,

今天碰见根据条件进行list遍历remove的问题,第一时间就是简单for循环remove,只知道这么写不行,不安全,可是为什么呢?你想过吗?下面就关于list遍历remove的问题,深挖一下!

一、几种常见的遍历方式

1、普通for循环

2、高级for循环

3、iterator和removeif

4、stream()

5、复制

6、普通for循环 --> 倒序方式

二、源码篇

1、普通for循环出错原因

public boolean remove(object o) {
  if (o == null) {
    for (int index = 0; index < size; index++)
      if (elementdata[index] == null) {
        fastremove(index);
        return true;
      }
  } else {
    for (int index = 0; index < size; index++)
      if (o.equals(elementdata[index])) {
        fastremove(index);
        return true;
      }
  }
  return false;
}
/*
 * private remove method that skips bounds checking and does not
 * return the value removed.
 */
private void fastremove(int index) {
  modcount++;
  int nummoved = size - index - 1;
  if (nummoved > 0)
    //remove会导致之后的元素往前移动,而下标不改变时就会出现bug
    system.arraycopy(elementdata, index+1, elementdata, index,
             nummoved);
  elementdata[--size] = null; // clear to let gc do its work
}

我们在删除某个元素后,list的大小发生了变化,这时候你的的索引也会发生变化,这时就会导致你在遍历的时候漏掉某些元素。
比如当你删除第1个元素后,我们如果还是继续根据索引访问第2个元素时,因为删除的关系,后面的元素都往前移动了一位,所以实际访问的是第3个元素。
所以这种方式可以用在删除特定的一个元素时使用,但不适合循环删除多个元素时使用。

2、高级for循环出错原因

foreach其实是用迭代器来进行遍历的,而在遍历时直接使用arraylist的remove方法会导致什么问题呢?

可以再看一下fastremove和迭代器遍历的内部代码:

最后导致抛出上面异常的其实就是这个,简单说,调用list.remove()方法导致modcount和expectedmodcount的值不一致而报异常 

final void checkforcomodification() {
  if (modcount != expectedmodcount)
    throw new concurrentmodificationexception();
}
//调用next时会调用checkforcomodification方法检查 这两个字段
//而fastremove里面只对modcount 进行了改变 导致抛出异常
public e next() {
  checkforcomodification();
  int i = cursor;
  if (i >= size)
    throw new nosuchelementexception();
  object[] elementdata = arraylist.this.elementdata;
  if (i >= elementdata.length)
    throw new concurrentmodificationexception();
  cursor = i + 1;
  return (e) elementdata[lastret = i];
}

所以遍历时remove并不适用于foreach。

3、java8中新方法removeif

//内部其实就是迭代器遍历
default boolean removeif(predicate<? super e> filter) {
  objects.requirenonnull(filter);
  boolean removed = false;
  final iterator<e> each = iterator();
  while (each.hasnext()) {
    if (filter.test(each.next())) {
      each.remove();
      removed = true;
    }
  }
  return removed;
}

和迭代器差不多,内部实现也是迭代器。

三、总结

1、在不考虑内存大小会不会出现oom的时候,采取复制一个新的list的方法速度更快,适用于集合中对象不算多的时候,毕竟只需要add操作。

2、当集合中元素过多时,复制list就显得有些笨重了,采用迭代器的方式进行遍历较快一些,并且不用关注小角标的变化。

3、不考虑性能的时候使用removeif方法,代码简洁明了。

4、当要针对角标进行元素的remove时,使用倒序遍历的方式最为妥当。

到此这篇关于java中list遍历删除元素remove()的方法的文章就介绍到这了,更多相关java list遍历删除元素内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《Java中List遍历删除元素remove()的方法.doc》

下载本文的Word格式文档,以方便收藏与打印。