Techniques for Replacing Elements in Java Collections

Using the Collections Class to Replace Elements in a Collection

The Collections class is part of the Java Collections Framework and provides numerous static methods for manipulating or returning collection objects. However, the Collections class does not offer a direct method to replace all elements in a collection. You can achieve this by using loops or other approaches.

To replace every element in a collection, you need to iterate through the collection and perform operations on each element. The following example demonstrates how to use ArrayList and Collections.replaceAll to replace all elements in a list:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ElementReplacementExample {
    public static void main(String[] args) {
        // Create an ArrayList
        List<String> fruitList = new ArrayList<>();
        fruitList.add("Apple");
        fruitList.add("Banana");
        fruitList.add("Cherry");

        // Display the original list
        System.out.println("Original List: " + fruitList);

        // Replace all elements in the list using Collections.replaceAll
        Collections.replaceAll(fruitList, element -> "Updated_" + element);

        // Display the modified list
        System.out.println("List after replacement: " + fruitList);
    }
}

In this example, the Collections.replaceAll method takes a list and a function as arguments. The function defines how to replace each element in the list. Here, each element is replaced with "Updated_" concatenated with the original element.

Note that Collections.replaceAll modifies the original list in place and does not return a new list.

To replace specific elements in a collection, you can use methods like Collection.removeIf or List.replaceAll. The removeIf method accepts a predicate (a function returning a boolean) and removes all elements that satisfy it. The replaceAll method takes a function to replace elements that meet a condition. For instance:

import java.util.ArrayList;
import java.util.List;

public class SpecificElementReplacement {
    public static void main(String[] args) {
        // Create an ArrayList
        List<String> fruitList = new ArrayList<>();
        fruitList.add("Apple");
        fruitList.add("Banana");
        fruitList.add("Cherry");

        // Display the original list
        System.out.println("Original List: " + fruitList);

        // Replace all occurrences of "Banana" with "Mango"
        fruitList.replaceAll(element -> element.equals("Banana") ? "Mango" : element);

        // Display the modified list
        System.out.println("List after replacement: " + fruitList);
    }
}

In this example, the replaceAll method replaces all elements equal to "Banana" with "Mango".

Replacing Elements in Java Collections Using Lambda Expressions

In Java, if you want to use lambda expressions to replace elements in a collection, you can traverse the collection and apply the map() method. This method creates a new collection where elements are transformed based on a provided function. The map() method does not modify the original collection.

Below is an example showing how to use lambda expressions and the map() method to replace each element in a List:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class LambdaReplacementExample {
    public static void main(String[] args) {
        // Create a list of strings
        List<String> initialList = Arrays.asList("Apple", "Banana", "Cherry");

        // Display the original list
        System.out.println("Original List: " + initialList);

        // Use lambda expression and map method to replace each element
        List<String> modifiedList = initialList.stream()
                .map(item -> "Updated_" + item) // Lambda expression for element transformation
                .collect(Collectors.toList()); // Collect results into a new list

        // Display the transformed list
        System.out.println("List after replacement: " + modifiedList);
    }
}

In this example, the map() method accepts a lambda expression that defines how to transform each element in the collection. Here, each element is replaced with "Updated_" concatenated with the original element. The collect(Collectors.toList()) method gathers the stream elements into a new List.

Note that map() returns a new stream and does not alter the original collection. To replace elements in the original collection, you can convert the new stream back to a collection and assign it to the original reference if applicable:

initialList = initialList.stream()
        .map(item -> "Updated_" + item)
        .collect(Collectors.toList());

However, be aware that if initialList is an immutable list (e.g., created via Collections.unmodifiableList()), this operation will throw an UnsupportedOperationException. In such cases, you must create a new list to store the replaced elements.

Tags: java Collections Lambda Expressions Stream API Element Replacement

Posted on Sun, 09 Aug 2026 16:08:06 +0000 by shoz