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 the list by 1.

import java.util.LinkedList;
public class Main {
  public static void main(String[] args) {
    LinkedList<Integer> numbers = new LinkedList<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 an operation applied to that item. This operation can be expressed through a lambda expression compatible with Java’s UnaryOperator interface.


Syntax

public void replaceAll(UnaryOperator operator)

Parameter Values

Parameter

Description

operator

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