Java ArrayList常用方法

Java ArrayList removeIf()方法  用于删除(移除)所有满足特定条件的arraylist元素。

语法

语法如下:
boolean removeIf(Predicate<? super E> filter) 

    参数

    • filter :  过滤方法,用于判断要删除的元素

    返回值

    成功过滤或者说是删除返回true,失败返回false。

    例子

    介绍2个例子了解该函数的使用方法

    例1

    过滤指定的元素,这里判断字符串中是否包含字符串'so'

    import java.util.ArrayList;
    import java.util.List;
    
    public class Test {
        public static void main(String[] args) {
            List<String> arrayList= new ArrayList<String>();
            arrayList.add("yxjc123.com");
            arrayList.add("taobao.com");
            arrayList.add("baidu.com");
            arrayList.add("sohu.com");
    
            arrayList.removeIf(item->item.contains("so"));//过滤sohu.com
            System.out.println("移除元素后为: "+arrayList);
        }
    } 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    输出:

    移除元素后为: [yxjc123.com, taobao.com, baidu.com]

    例2

    过滤对象元素,过滤条件是age>23,如下:

    import java.util.ArrayList;
    import java.util.List;
    
    public class Test2 {
        public static void main(String[] args) {
            List<Employee> arrayList= new ArrayList<Employee>();
            Employee e1 = new Employee("张三","male", 22);
            Employee e2 = new Employee("李四","male", 24);
            Employee e3 = new Employee("王五","male", 26);
    
            arrayList.add(e1);
            arrayList.add(e2);
            arrayList.add(e3);
    
            arrayList.removeIf(item->item.getAge()>23);//过滤年龄大于23的
            System.out.println("移除元素后为: "+arrayList);
        }
    }
    
    //JavaBean.java
    class  Employee{
    
        private String name;
        private String sex;
        private Integer age;
    
        @Override
        public String toString() {
            return "Employee{" +
                    "name='" + name + '\'' +
                    ", sex='" + sex + '\'' +
                    ", age=" + age +
                    '}';
        }
    
        public Employee(String name, String sex, Integer age) {
            this.name = name;
            this.sex = sex;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    } 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    输出:
    移除元素后为: [Employee{name='张三', sex='male', age=22}]