Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java ArrayList replaceAll()

ArrayList replaceAll() transform each element in the list by applying a lambda expression or UnaryOperator implementation.

Lokesh Gupta

January 12, 2023

Java ArrayList
Java ArrayList
ArrayList

In Java, ArrayList.replaceAll() retains only the elements in this list that are present in the specified method argument collection. Rest all elements are removed from the list. This method is exactly the opposite of removeAll() method.

1. ArrayList.replaceAll() API

The replaceAll() method takes a single argument of type UnaryOperator. The UnaryOperator interface is a functional interface with a single abstract method named apply() that returns a result of the same object type as the operand. We have seen UnaryOperator in action in usually lambda expressions taking a single argument in form of 'x-> do something with x'.

public void replaceAll(UnaryOperator<E> operator);

Method parameter – UnaryOperator expression.
Method returns – void.
Method throws – ConcurrentModificationException if the list is modified while replaceAll() is not finished.

2. ArrayList.replaceAll() Example

The following Java programs use replaceAll() method to change all list items to lowercase using a lambda expression.

2.1. Using Inline Expression

We can use the inline lambda expressions in case we have to execute only a single statement.

ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));
 
System.out.println(alphabets);
 
alphabets.replaceAll( e -> e.toLowerCase() );  
 
System.out.println(alphabets);

Program output.

[A, B, C, D, E]
[a, b, c, d, e]

2.2. Custom Implementation of UnaryOperator

Create a new class that implements UnaryOperator to execute a more complex logic on each element of the arraylist.

class MyOperator implements UnaryOperator<String> {

  @Override
  public String apply(String t) {

    //Add your custom logic here
    return t.toLowerCase();
  }
}

Now we can use the MyOperator as follows:

ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));
System.out.println(alphabets);
 
alphabets.replaceAll( new MyOperator() );  
System.out.println(alphabets);

Program output.

[A, B, C, D, E]
[a, b, c, d, e]

That’s all for the ArrayList replaceAll() method in Java.

Happy Learning !!

Read More: ArrayList Java Docs

Sourcecode on Github

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

ArrayList Methods

  • ArrayList add()
  • ArrayList addAll()
  • ArrayList clone()
  • ArrayList contains()
  • ArrayList ensureCapacity()
  • ArrayList forEach()
  • ArrayList get()
  • Arraylist indexOf()
  • Arraylist lastIndexOf()
  • ArrayList listIterator()
  • ArrayList remove()
  • ArrayList removeAll()
  • ArrayList removeIf()
  • ArrayList retainAll()
  • ArrayList spliterator()
  • ArrayList subList()
  • ArrayList toArray()

ArrayList Examples

  • Initialize Arraylist
  • Iteration
  • Add/replace Element
  • Add Multiple Elements
  • Check Empty List
  • Remove Element
  • Replace Element
  • Empty ArrayList
  • Synchronized ArrayList
  • Compare two lists
  • Remove duplicates
  • Merge two lists
  • Serialize a List
  • Swap two elements
  • Convert Array to ArrayList
  • Convert HashSet to ArrayList
  • Convert LinkedList to ArrayList
  • ArrayList vs LinkedList
  • ArrayList vs Vector

Table of Contents

  • 1. ArrayList.replaceAll() API
  • 2. ArrayList.replaceAll() Example
    • 2.1. Using Inline Expression
      • 2.2. Custom Implementation of UnaryOperator
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java List retainAll()

Next

ArrayList removeIf(): Remove Elements Matching a Condition

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap