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

ParameterDescriptionRequired/Optional
listIt is the list to be filled with the specified elements.Required
objIt 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: [JavaTpoint, JavaTpoint, JavaTpoint]

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]





Latest Courses