CopyOnArrayList replaceAll() method in Java with Examples
Last Updated :
08 Jan, 2019
The java.util.concurrent.CopyOnArrayList.replaceAll() method in Java replaces each element of this list with the result of applying the operator to the element.
Syntax:
public void replaceAll(UnaryOperator operator)
Parameters: This method accepts a mandatory parameter operator which is to be applied to each element.
Return Type: This method has no return value.
Below Programs illustrate the replaceAll() method of CopyOnArrayList in Java:
Program 1: This program involves CopyOnArraylist replaceAll() method of String Type:
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
import java.util.function.UnaryOperator;
public class GFG {
public static void main(String[] args)
{
CopyOnWriteArrayList<String> ArrLis1
= new CopyOnWriteArrayList<String>();
ArrLis1.add( "White" );
ArrLis1.add( "Red" );
ArrLis1.add( "Blue" );
ArrLis1.add( "Green" );
System.out.println( "CopyOnWriteArrayList: "
+ ArrLis1);
ArrLis1.replaceAll( new MyOperator());
System.out.println( "After replacement CopyonWriteArrayList: "
+ ArrLis1);
}
}
class MyOperator implements UnaryOperator<String> {
public String apply(String t)
{
return t.replaceAll( "Red" , "White" );
}
}
|
Output:
CopyOnWriteArrayList: [White, Red, Blue, Green]
After replacement CopyonWriteArrayList: [White, White, Blue, Green]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...