Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the CopyOnWriteArrayList.replaceAll method() in Java?

abhilash

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.

What is CopyOnWriteArrayList?

  • The CopyOnWriteArrayList method is a thread-safe implementation of the ArrayList without the requirement for synchronization.
  • The whole content of the CopyOnWriteArrayList method is duplicated on to a new internal copy when we use any of the altering methods, such as add() or delete(). Hence, the list can be iterated in a safe way, without any fear of losing data, and can be modified simultaneously.
  • The data structure’s properties make it particularly helpful in situations when we iterate it more frequently than we alter it.
  • If adding items is a typical activity, then the CopyOnWriteArrayList method isn’t the appropriate data structure to use, because the extra copies will almost always result in poor performance.

replaceAll() is an instance method of the CopyOnWriteArrayList class, which takes an implementation of the UnaryOperator functional interface. This method is used to replace every element in the list with the result of applying the UnaryOperator function to that element. The replaceAll functionality is in-place in nature, that is, the original list is modified.

The replaceAll method is defined in the CopyOnWriteArrayList class. The CopyOnWriteArrayList class is defined in the java.util.concurrent package. To import the CopyOnWriteArrayList class, we check the following import statement:

import java.util.concurrent.CopyOnWriteArrayList;

Syntax


public void replaceAll(UnaryOperator<E> operator)

Parameters

  • UnaryOperator<E> operator: The operator/function to apply to each element in the list.

Return value

This method doesn’t return any value(s).

Code example

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.UnaryOperator;
public class Main {
public static void main(String[] args){
CopyOnWriteArrayList<String> copyOnWriteArrayListObject = new CopyOnWriteArrayList<>();
copyOnWriteArrayListObject.add("edpresso");
copyOnWriteArrayListObject.add("educative");
copyOnWriteArrayListObject.add("coding-interview");
copyOnWriteArrayListObject.add("courses");
UnaryOperator<String> lengthOfElements = str -> String.valueOf(str.length());
System.out.println("Original List - " + copyOnWriteArrayListObject);
copyOnWriteArrayListObject.replaceAll(lengthOfElements);
System.out.println("Modified List - " + copyOnWriteArrayListObject);
}
}
Implementation of the "replaceAll()" method

Code explanation

  • Lines 1–2: We import the relevant classes.
  • Line 7: We create an instance of the CopyOnWriteArrayList class, which we call copyOnWriteArrayListObject.
  • Lines 9–12: We insert elements into the copyOnWriteArrayListObject, using the add() method.
  • Line 14: We define an implementation of the UnaryOperator called lengthOfElements, which takes in a string and returns the length of that string.
  • Line 16: We print the content of the copyOnWriteArrayListObject to the console.
  • Line 18: We replace every element in the copyOnWriteArrayListObject with the return value of the lengthOfElements method.
  • Line 20: We print the content of the copyOnWriteArrayListObject to the console.

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