Home » Java Collections fill() Method with Examples

Java Collections fill() Method with Examples

by Online Tutorials Library

Java Collections fill() Method

The fill() method of Java Collections class is used to replace all of the elements of the specified list with the specified elements. This method filled all the elements with the same values.

Syntax

Following is the declaration of fill() method:

Parameter

Parameter Description Required/Optional
list It is the list to be filled with the specified elements. Required
obj It is the element to fill the specified list. Required

Returns

The fill() method does not return anything.

Exceptions

UnsupportedOperationException– This exception will be thrown 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:

List elements before Replacements: [AAA, BBB, CCC]  List elements after Replacements: [TutorAspire, TutorAspire , TutorAspire ]  

Example 2

Test it Now

Output:

551 551 551 551   

Example 3

Test it Now

Output:

Before Fill: [Hi, Hi, Hi, Hi, Hi, Hi]  After Fill: [Hello, Hello, Hello, Hello, Hello, Hello]  

You may also like