Replace all list Java

  • Home
  • Java Tutorial
  • Java.util.ArrayList Class
  • ArrayList Class Methods
  • trimToSize Method
  • ensureCapacity Method
  • size Method
  • isEmpty Method
  • contains Method
  • indexOf Method
  • lastIndexOf Method
  • clone Method
  • toArray Method
  • toArray[T[] a] Method
  • get Method
  • set Method
  • add Method [Object]
  • add[int index, E element] Method
  • remove[int index] Method
  • remove[Object o] Method
  • clear Method
  • addAll[Collection c] Method
  • removeRange Method
  • removeAll Method
  • retainAll Method
  • listIterator[index] Method
  • listIterator[] Method
  • iterator Method
  • subList Method
  • foreach Method
  • spliterator
  • removeIf
  • replaceAll
  • sort

Java ArrayList.replaceAll[] Method

Last update on February 26 2020 08:07:31 [UTC/GMT +8 hours]

public void replaceAll[UnaryOperator operator]

The replaceAll[] method is used to replace each element of this list with the result of applying the operator to that element.
Errors or runtime exceptions are thrown by the operator are relayed to the caller.

Package: java.util

Java Platform: Java SE 8

Syntax:

replaceAll[UnaryOperator operator]

Parameters:

NameDescription
operatorThe operator to apply to each element.

Return Value:

This method does not return any value.

Pictorial presentation of ArrayList.replaceAll[] Method


Example: Java ArrayList.replaceAll[] Method

The following example removes all of the elements of this collection that satisfy the given predicate.

import java.util.function.*; class MyOperator implements UnaryOperator{ T varc1; public T apply[T varc]{ return varc1; } }
import java.util.*; public class test { public static void main[String[] args] { ArrayList color_list; MyOperator operator; color_list = new ArrayList []; operator = new MyOperator []; operator.varc1 = "White"; // use add[] method to add values in the list color_list.add["White"]; color_list.add["Black"]; color_list.add["Red"]; color_list.add["White"]; color_list.add["Yellow"]; color_list.add["White"]; System.out.println["List of Colors"]; System.out.println[color_list]; // Replace all colors with White color color_list.replaceAll[operator]; System.out.println["Color list, after replacing all colors with White color :"]; System.out.println[color_list]; } }

Output:

F:\java>javac test.java F:\java>java test List of Colors [White, Black, Red, White, Yellow, White] Color list, after replacing all colors with White color : [White, White, White, White, White, White]

Previous:removeIf
Next:sort


  • New Content published on w3resource:
  • Scala Programming Exercises, Practice, Solution
  • Python Itertools exercises
  • Python Numpy exercises
  • Python GeoPy Package exercises
  • Python Pandas exercises
  • Python nltk exercises
  • Python BeautifulSoup exercises
  • Form Template
  • Composer - PHP Package Manager
  • PHPUnit - PHP Testing
  • Laravel - PHP Framework
  • Angular - JavaScript Framework
  • Vue - JavaScript Framework
  • Jest - JavaScript Testing Framework

Video liên quan

Chủ Đề