ArrayList replaceAll Method Tutorial

void replaceAll(UnaryOperator<E> operator)

The replaceAll method replaces all elements of the current ArrayList instance with the result of the UnaryOperator expression (say what??? - I'll explain). This method may not produce the results that you think it should as this method replaces each and every element with the result of an expression. There is another replaceAll method in the Collections class where you can replace certain elements with other values - I'll demonstrate that as well. In order to use this method, you will need to have a basic understanding of lambda expressions. If you are not familiar with lambda expressions, the following tutorials will teach you what you need to know in order to use this method:

The UnaryOperator interface is a functional interface that has a single abstract method named apply that returns a result of the same object type as the operand. The UnaryOperator interface does not have an explicitly defined abstract method, rather it inherits the abstract method apply from it's superinterface, Function.
public interface UnaryOperator<T> extends Function<T,R> {
      R apply(T t);
}
So basically we can pass a UnaryOperator based lambda expression as the argument for this method to do its thing. I'll demonstrate exactly how the UnaryOperator interface works in the source code.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ArrayListReplaceAll
C:\Java>cd ArrayListReplaceAll
C:\Java\ArrayListReplaceAll>Notepad ArrayListReplaceAll.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.util.*;
import java.util.function.*;

class ArrayListReplaceAll {
    public static void main(String args[]) {

        FunctionalInterface<Integer> fi;
        fi = (x) -> x + 10 ;        
        Integer i = 31;
        System.out.println(fi.apply(i));

        UnaryOperator<Integer> uo = (x) -> x * x;
        i = 20;
        System.out.println(uo.apply(i));   
    
        UnaryOperator<String> suo = (x) -> x.toUpperCase();
        String s = "Javac Java";
        System.out.println(suo.apply(s));       
    
        ArrayList<String> states = new ArrayList<String>();
        states.addAll(Arrays.asList("Alaska", "Alabama", "California", "Colorado", "Hawaii", "New York"));
        System.out.println("states = " + states);
        UnaryOperator<String> uoRef = (x) -> x.toUpperCase();
        states.replaceAll(uoRef);
        System.out.println("states.ReplaceAll(p) = " + states);
        System.out.println();

        states.replaceAll( (String x) -> x.toLowerCase());
        System.out.println("states.ReplaceAll( (String x) -> x.toLowerCase()) = " + states);

        Collections.replaceAll(states,"california","EarthquakeVille");
        System.out.println("Collections.replaceAll(states,\"california\",\"EarthquakeVille\") = " + states);
        System.out.println();

        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.addAll(Arrays.asList(5, 62, 107));
        System.out.println("numbers = " + numbers);
        numbers.replaceAll((x) -> x + 10);
        System.out.println("numbers.replaceAll((x) -> x + 10) = " + numbers);
        System.out.println();

    }
}

interface FunctionalInterface<T> {
    T apply(T t) ;
}

Now switch back to the command prompt (CMD) and type in javac ArrayListReplaceAll.java and press Enter.
Now type in java ArrayListReplaceAll and press Enter.


C:\Java\ArrayListReplaceAll>javac ArrayListReplaceAll.java
C:\Java\ArrayListReplaceAll>java ArrayListReplaceAll
41
400
JAVAC JAVA
states = [Alaska, Alabama, California, Colorado, Hawaii, New York]
states.ReplaceAll(p) = [ALASKA, ALABAMA, CALIFORNIA, COLORADO, HAWAII, NEW YORK]

states.ReplaceAll( (String x) -> x.toLowerCase()) = [alaska, alabama, california, colorado, hawaii, new york]
Collections.replaceAll(states,\"California\",\"EarthquakeVille\") = [alaska, alabama, EarthquakeVille, colorado, hawaii, new york]

numbers = [5, 62, 107]
numbers.replaceAll((x) -> x + 10) = [15, 72, 117]


Final thoughts

None


Tutorials