Java LinkedList replaceAll() Method with Examples

In the previous article, we have discussed about Java LinkedList forEach() Method with Examples

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

Java LinkedList replaceAll() Method with Examples

Let’s see replaceAll() method with suitable examples.

Method-1: replaceAll()- Using lambda function

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

Syntax:

LinkedListName.replaceAll(action)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • action refers to the modification that will be reflected in new LinkedLis after replacement.

Example: Java LinkedList replaceAll() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add String elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Now replace all the elements of old LinkedList by converting those into upper case elements(any action you can perform as per requirement) by using replaceAll() method.
  • Print the new LinkedList after replacement.

Program:

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

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

Method-2: replaceAll(Collection c, int oldValue, int newValue)

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

Syntax:

LinkedListName.replaceAll(Collection c, int oldValue, int newValue)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • (Collection c, int oldValue, int newValue) refers to the collection in which replacement will take place, and the old value which will be replaced by the new value.

Example: Java LinkedList replaceAll() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add Integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Now replace all the old elements by specified new element by using replaceAll(Collection c, int oldValue, int newValue) method.
  • Print the new LinkedList.

Program:

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

Output:

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

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: