74
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
Output:
List elements before Replacements: [AAA, BBB, CCC] List elements after Replacements: [TutorAspire, TutorAspire , TutorAspire ]
Example 2
Output:
551 551 551 551
Example 3
Output:
Before Fill: [Hi, Hi, Hi, Hi, Hi, Hi] After Fill: [Hello, Hello, Hello, Hello, Hello, Hello]
Next TopicJava Collections Class