ArrayList中的removeIf

本文探讨了ArrayList的removeIf方法,该方法根据指定条件删除元素。ArrayList在Java 1.8后引入此功能,不同于clear方法的无条件清空,removeIf按条件筛选并移除元素。此外,还介绍了ArrayList在集合框架中的位置,如继承关系和实现的接口。
摘要由CSDN通过智能技术生成
转载请标明出处^_^
原文首发于: www.zhangruibin.com
本文出自于: RebornChang的博客

ArrayList

ArrayList中的removeIf

ArrayList 继承了 AbstractList,实现了 List、 RandomAccess、 Cloneable、java.io.Serializable接口;
AbstractList继承了 AbstractCollection,实现了 List接口;
AbstractCollection实现了Collection接口;
Collection接口继承了Iterable接口;
Collection为顶级集合接口,实现了Iterable接口,可以迭代。

Collction接口中在Jdk1.8之后新增了个removeIf,有啥用?
removeIf源码@since 1.8:

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;
   }

ArrayList对其进行重写:

@Override
   public boolean removeIf(Predicate<? super E> filter) {
       //首先要验证规则不能为空
       Objects.requireNonNull(filter);
       // figure out which elements are to be removed
       // any exception thrown from the filter predicate at this stage
       // will leave the collection unmodified
       //计数器,记录revove掉的次数
       int removeCount = 0;
       //声明要rem
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值