Arraylist replace java – Java ArrayList replaceAll() Method with Example

Arraylist replace java: In the previous article we have discussed about Java ArrayList removeIf() Method with Example

In this article we are going to see the use Java ArrayList replaceAll() method along with suitable examples.

Java ArrayList replaceAll() Method with Example

Replace element in arraylist java: Let’s see replaceAll() method with suitable examples.

Method-1: replaceAll() – using lambda function

Java arraylist replace: This java.util.ArrayList.replaceAll() method replace all the old element of a collection to the new elements within the arraylist.

Syntax:

arrayListName.replaceAll()

Where,

  • arrayListName refers to the name of your ArrayList.

Approach: 

  • Create a new ArrayList of type String.
  • Add String elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Replace the previous arrayList elements using replaceAll() method.
  • e -> e.toUpperCase() refers to the lambda function which will replace each elements into Upper Case.
  • Print the new ArrayList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a ArrayList of string datatype
        ArrayList<String> arr1 = new ArrayList<String>();
        // Adding some elements to the ArrayList
        arr1.add("monitor");
        arr1.add("mouse");
        arr1.add("key board");
        arr1.add("speaker");
        arr1.add("printer");
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList 1 are: "+arr1);
        // replacing the old elements into uppercase elements using replaceAll() method
        arr1.replaceAll(c -> c.toUpperCase());
        // Prints the new ArrayList elements
        System.out.println("The new elements of ArrayList 1 are: "+arr1);
    }
}
Output:

The elements of ArrayList 1 are: [monitor, mouse, key board, speaker, printer]
The new elements of ArrayList 1 are: [MONITOR, MOUSE, KEY BOARD, SPEAKER, PRINTER]

Method-2: Java ArrayList replaceAll(Collection col, int oldValue, int newValue) Method

This java.util.ArrayList.replaceAll(Collection col, int oldValue, int newValue) method is used to replace all the old specified value into new specified value.

Syntax:

Collections.replaceAll(Collection col, int oldValue, int newValue)

Where,

  • arrayListName refers to the name of your ArrayList.
  • Collection col refers to the list on which you want to perform the replace operation.
  • int oldValue refers to old list element which will be replaced with a new element.
  • int newValue refers to new list element which will replace the old list element.

Approach:

  • Create a new ArrayList of type Integer.
  • Add Integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Replace the previous arrayList elements using replaceAll(Collection col, int oldValue, int newValue) method.
  • Print new arraylist

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a ArrayList of string datatype
        ArrayList<Integer> arr1 = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr1.add(1);
        arr1.add(2);
        arr1.add(3);
        arr1.add(1);
        arr1.add(7);
        arr1.add(5);
        arr1.add(1);
        arr1.add(1);
        arr1.add(9);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList 1 are: "+arr1);
        //Replace '1' with '8' using replaceAll() method.
        Collections.replaceAll(arr1, 1, 8);  
        System.out.println("Value after replace :"+ arr1); 
    }
}
Output:

The elements of ArrayList 1 are: [1, 2, 3, 1, 7, 5, 1, 1, 9]
Value after replace :[8, 2, 3, 8, 7, 5, 8, 8, 9]

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: