Java ArrayList replaceAll() Function

replaceAll

What is an ArrayList?

Arraylist is a part of collection framework in java. It contains some inbuilt function like replaceAll() function which can be found in the java.util.ArrayList Package.
It implements the List interface of the collections framework.

Here, in the page we will discuss about the ArrayList’s replaceAll() Method in java. To  know more about ArrayList in java, you can click on the button given below.

ArrayList replaceAll Function:

Arraylist contains several inbuilt functions that are used to perform several operations on arraylist in Java. replaceAll function is also an inbuilt method of class ArrayList.
The replaceAll function is used to Replaces each element of the given list with the result of applying the operator to that element.

Syntax:

arraylist_name.replaceAll(UnaryOperator operator)

Definition of Parameters:

Return Type :

replaceAll Function

clear() Function in ArrayList

clone() Function in ArrayList

add() Function in ArrayList

addall() Function in ArrayList

Example for Replacing Elements of ArrayList of Integers:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        ArrayList<Integer> Prep = new ArrayList<>();
        Prep.add(1);
        Prep.add(3);
        Prep.add(5);
        Prep.add(7);
        
        System.out.println("ArrayList Prep : " + Prep);
        Prep.replaceAll(e -> e*2);
        System.out.println("After Performing Operation on Prep : " + Prep);
    }
}
Output:

ArrayList Prep : [1, 3, 5, 7]
After Performing Operation on Prep : [2, 6, 10, 14]

In the above Example, We had taken an ArrayList “Prep” of integer type of odd numbers, we are replacing all the elements of ArrayList to even by performing the multiplication operation on all the elements of the ArrayList.

Example for Replacing Elements of ArrayList of Strings:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        ArrayList<String> Prep = new ArrayList<>();
        Prep.add("C");
        Prep.add("C++");
        Prep.add("Java");
        Prep.add("Python");
        
        System.out.println("ArrayList Prep : " + Prep);
        Prep.replaceAll(e -> e + " Language");
        System.out.println("After Performing Operation on Prep : " + Prep);
    }
}
Output:
ArrayList Prep : [C, C++, Java, Python]
After Performing Operation on Prep : [C Language, C++ Language, Java Language, Python Language]

In the above Example. we have created an ArrayList of String Type, we have performed the addition operation on the ArrayList by adding a String “Language” to every element of the ArrayList. 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription