Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the CopyOnWriteArrayList.removeIf method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The removeIf method loops through all the elements of the CopyOnWriteArrayList object and removes the elements that match the condition of the predicatea functional interface that takes one argument and returns either true or false based on the defined condition. function, which is sent as the argument to the removeIf method.

Note: CopyOnWriteArrayList is a thread-safe version of an ArrayList. For all the write operations like add and set, CopyOnWriteArrayList makes a fresh copy underlying array and performs the operation on the cloned array. Due to this, the performance is slower when compared to ArrayList. Read more about CopyOnWriteArrayList here.

Syntax

The syntax of the removeIf method is given below:

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

Parameters

The removeIf method takes the Predicate function as an argument.

Return value

Thie method returns true if any one element is removed from the list.

Example

The code below demonstrates the use of the removeIf method.

import java.util.concurrent.CopyOnWriteArrayList;
class RemoveIf {
public static void main( String args[] ) {
// Creating CopyOnWriteArrayList which can contain string elements
CopyOnWriteArrayList<Integer> numbers = new CopyOnWriteArrayList<>();
// add elements to the list
numbers.add(1);
numbers.add(-10);
numbers.add(30);
numbers.add(-3);
System.out.println("The numbers list is " + numbers);
// use removeIf to remove negative numbers
numbers.removeIf(number -> number < 0);
System.out.println("After removing negative elements, the numbers list is => " + numbers);
}
}
Using the removeIf method

Explanation

In the code above:

  • Line 5: We create a CopyOnWriteArrayList object with the name list.

  • Lines 8 to 11: We add four elements (1, -10, 30, -3) to the list object.

  • Line 15: We call the removeIf method with a predicate function as an argument. The predicate function tests against each element of the numbers object. In the predicate function, we check if number < 0. For negative numbers, the predicate function returns true, and the elements are removed. After we call the removeIf method, all of the negative numbers from the numbers object are removed.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring