Java ConcurrentLinkedQueue removeIf() Method

The removeIf() method of ConcurrentLinkedQueue class removes the elements of this queue that satisfies the given predicate filter.

Syntax:

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

Parameters:

The parameter filter is a predicate which returns true for the elements to be removed.

Specified By:

The removeIf() method of ConcurrentLinkedQueue class is specified by:

removeIf in interface Collection<E>.

Return Value:

The removeIf () method returns a Boolean value 'true' if the collection has removed any element, else it returns 'false'.

Throws:

It throws NullPointerException if the specified filter is null.

Example 1

import java.util.concurrent.ConcurrentLinkedQueue; import java.util.function.Predicate; public class ConcurrentLinkedQueueRemoveIfExample1 { public static void main(String[] args) { ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>(); for (int i=1;i<21;i++){ queue.add(i);} System.out.println("Total no : "+ queue); //removes all the elements which satisfies the predicate filter Predicate<Integer> pr= a->(a>10); queue.removeIf(pr ); System.out.println(" Number less than 11 = "+queue); Test it Now

Output:

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

Example 2

import java.util.concurrent.ConcurrentLinkedQueue; import java.util.function.Predicate; public class ConcurrentLinkedQueueRemoveIfExample2 { public static void main(String[] args) { ConcurrentLinkedQueue<IsVoter> queue = new ConcurrentLinkedQueue<IsVoter>(); IsVoter isVoter1 = new IsVoter("Reema",18); IsVoter isVoter2 = new IsVoter("Raj",7); IsVoter isVoter3 = new IsVoter("Kajol",37); IsVoter isVoter4 = new IsVoter("Ravi",47); IsVoter isVoter5 = new IsVoter("Varun",17); queue.add(isVoter1); queue.add(isVoter2); queue.add(isVoter3); queue.add(isVoter4); queue.add(isVoter5); //removes all the elements which satisfies the Predicate filter Predicate<IsVoter> pr= (IsVoter age) ->(age.age < 18); queue.removeIf(pr ); System.out.println(" People eligible to vote : "); for (IsVoter xyz : queue ){ System.out.println(xyz); class IsVoter { String name; int age; public IsVoter(String name, int age) { this.name = name; this.age = age; public String toString() { return "Name = " + this.name + " Age = " + this.age; Test it Now

Output:

People eligible to vote : Name = Reema Age = 18 Name = Kajol Age = 37 Name = Ravi Age = 47

Example 3

import java.util.concurrent.ConcurrentLinkedQueue; import java.util.function.Predicate; public class ConcurrentLinkedQueueRemoveIfExample3 { public static void main(String[] args) { ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>(); for (int i=1;i<21;i++){ queue.add(i);} System.out.println("Total no : "+ queue); //removes all the elements which satisfies the predicate filter Predicate<Integer> pr=null; queue.removeIf(pr ); System.out.println(queue); Test it Now

Output:

Exception in thread "main" java.lang.NullPointerException at java.util.Objects.requireNonNull(Objects.java:203) Total no : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] at java.util.Collection.removeIf(Collection.java:410) at com.javaTpoint.ConcurrentLinkedQueueRemoveIfExample3.main(ConcurrentLinkedQueueRemoveIfExample3.java:13) Next Topic Java ConcurrentLinkedQueue