Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Replacing List Elements

Java Example – Replacing List Elements

Java Examples Java Examples

The following example demonstrates how to use the replaceAll() method of the Collections class to replace all specified elements in a List:

Main.java File

import

java

.

util

.*;

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

{

List

list

=

Arrays

.

asList

(

"

one Two three Four five six one three Four

"

.

split

(

"

"

)

)

;

System

.

out

.

println

(

"

List :

"

list

)

;

Collections

.

replaceAll

(

list

,

"

one

"

,

"

hundrea

"

)

;

System

.

out

.

println

(

"

replaceAll:

"

list

)

;

}

}

The output of the above code is:


List :[one, Two, three, Four, five, six, one, three, Four]
replaceAll: [hundrea, Two, three, Four, five, six, hundrea, three, Four]

Java Examples Java Examples