Home » Java Collections replaceAll() Method with Examples

Java Collections replaceAll() Method with Examples

by Online Tutorials Library

Java Collections replaceAll() Method

The replaceAll() method of Java Collections class is used to replace all occurrences of one specified value in a list with the other specified value.

Syntax

Following is the declaration of replaceAll() method:

Parameter

Parameter Description Required/Optional
list It is the list in which replacement is to be occur. Required
oldVal It is the old value which will be replaced. Required
newVal It is the new value with which oldVal is to be replaced. Required

Returns

The replaceAll() method returns true if the list contained one or more elements e such that (oldVal==null ? e==null : oldVal.equals(e)).

Exceptions

UnsupportedOperationException– It throws this exception if the specified list or its list-iterator does not support the set operation.

Compatibility Version

Java 1.4 and above

Example 1

Test it Now

Output:

Initial values are :[Java, SSSIT, HINDI100, Java]  Value after replace :[tutoraspire, SSSIT, HINDI100, tutoraspire]  

Example 2

Test it Now

Output:

Original List:- [one, two, one]  Boolean: true  Value after replace:- [three, two, three]  

Example 3

Test it Now

Output:

Original List: [10, 20, 10, 40, 50, 10]  Boolean: true  Value after Replace: [400, 20, 400, 40, 50, 400]  

You may also like