Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

replaceAll()

Example

Increment every number in a list by 1.

import java.util.ArrayList;
public class Main { 
  public static void main(String[] args) {
    ArrayList<Integer> list = new ArrayList<Integer>();
numbers.add(5);
    numbers.add(9);
    numbers.add(8);
    numbers.add(6);
    numbers.add(1);
    numbers.replaceAll( n -> n + 1 );
    System.out.println(numbers);
  }
}

Definition and Usage

The replaceAll() method substitutes each item in a list with the outcome of a specified operation on that item. This operation can be defined using a lambda expression that conforms to Java’s UnaryOperator interface.

For further understanding of lambda expressions, refer to our Java Lambda Expression tutorial.

Syntax

public void replaceAll(UnaryOperator operator)

Parameter Values

Parameter

Description

operator

Necessary: A UnaryOperator or lambda expression that acts on each item in the list.