当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Collection removeIf()用法及代码示例


Java 集合接口的 removeIf() 方法删除该队列中满足给定谓词过滤器的元素。

用法

public boolean removeIf (Predicate<?  super E>  filter)

参数

参数 'filter' 表示一个谓词,它为要删除的元素返回 true。

返回值:

如果集合删除了任何元素,removeIf() 方法返回一个布尔值 'true',否则返回 'false'。

抛出

UnsupportedOperationException- 如果无法从此集合中删除元素。

NullPointerException- 如果指定的过滤器为空。

例子1

import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Predicate;
public class JavaCollectionRemoveIfExample1 {
    public static void main(String[] args) {
        Collection<Integer> collection = new ConcurrentLinkedQueue<Integer>();
        for (int i=1;i<21;i++){
            collection.add(i);}
        System.out.println("Total no:"+ collection);
        //removes all the elements which satisfies the predicate filter
        Predicate<Integer> pr= a->(a%2!=0);
        //remove all the elements which do not come in 2's table
        collection.removeIf(pr );
        System.out.println(" Table of 2 = "+collection);
    }
}

输出:

Total no:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
 Table of 2 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

例子2

import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Predicate;
public class JavaCollectionRemoveIfExample2 {
    static int i=1;
    public static void main(String[] args) {
        Collection<IsPass> collection = new ConcurrentLinkedQueue<IsPass>();
        IsPass isVoter1 = new IsPass("Reema",98);
        IsPass isVoter2 = new IsPass("Raj",71);
        IsPass isVoter3 = new IsPass("Kajol",37);
        IsPass isVoter4 = new IsPass("Ravi",47);
        IsPass isVoter5 = new IsPass("Varun",17);
        collection.add(isVoter1);
        collection.add(isVoter2);
        collection.add(isVoter3);
        collection.add(isVoter4);
        collection.add(isVoter5);
        //removes all the elements which satisfies the Predicate filter
        Predicate<IsPass> pr= (IsPass marks) ->(marks.marks < 45);
        collection.removeIf(pr );
        System.out.println(" People who passed the exam:");
        for (IsPass xyz:collection ){
            System.out.println(i++ +". "+xyz);
        }
    }
}
class IsPass {
    String name;
    int marks;
    public IsPass(String name, int age) {
        this.name = name;
        this.marks = age;
    }
    public String toString() {
        return this.name + "  has passed the exam with " + this.marks +" marks.";
    }
}

输出:

People who passed the exam:
1. Reema  has passed the exam with 98 marks.
2. Raj  has passed the exam with 71 marks.
3. Ravi  has passed the exam with 47 marks.

例子3

import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Predicate;
public class JavaCollectionRemoveIfExample3 {
    public static void main(String[] args) {
        Collection<Character> collection = new ConcurrentLinkedQueue<Character>();
        char c;
        for(c='A';c<='Z';++c){
            collection.add(c);
        }
        System.out.println("Albabets:"+ collection);
        //removes all the elements which satisfies the predicate filter
        Predicate<Character> pr= a->(a!='A'&& a!='E'&& a!='I'&& a!='O'&& a!='U' );
        //remove all the non vowels alphabets
        collection.removeIf(pr );
        System.out.println(" Vowels = "+collection);
    }
}

输出:

Albabets:[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
 Vowels = [A, E, I, O, U]




相关用法


注:本文由纯净天空筛选整理自 Java Collection removeIf() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。