Collections fill() method in Java with Examples
The fill() method of java.util.Collections class is used to replace all of the elements of the specified list with the specified element.
This method runs in linear time.
Syntax:
public static void fill(List list, T obj)
Parameters: This method takes following argument as parameter
- list – the list to be filled with the specified element.
obj – The element with which to fill the specified list.Below are the examples to illustrate the fill() method
Example 1:
// Java program to demonstrate// fill() method// for String valueimportjava.util.*;publicclassGFG1 {publicstaticvoidmain(String[] argv)throwsException{try{// creating object of List<Integer>List<String> arrlist =newArrayList<String>();// Adding element to srclstarrlist.add("A");arrlist.add("B");arrlist.add("C");// print the elementsSystem.out.println("List elements before fill: "+ arrlist);// fill the listCollections.fill(arrlist,"TAJMAHAL");// print the elementsSystem.out.println("\nList elements after fill: "+ arrlist);}catch(IllegalArgumentException e) {System.out.println("Exception thrown : "+ e);}catch(NoSuchElementException e) {System.out.println("Exception thrown : "+ e);}}}chevron_rightfilter_noneOutput:List elements before fill: [A, B, C] List elements after fill: [TAJMAHAL, TAJMAHAL, TAJMAHAL]
Example 2:
// Java program to demonstrate// fill() method// for Integer valueimportjava.util.*;publicclassGFG1 {publicstaticvoidmain(String[] argv)throwsException{try{// creating object of List<Integer>List<Integer> arrlist =newArrayList<Integer>();// Adding element to srclstarrlist.add(20);arrlist.add(30);arrlist.add(40);// print the elementsSystem.out.println("List elements before fill: "+ arrlist);// fill the listCollections.fill(arrlist,500);// print the elementsSystem.out.println("\nList elements after fill: "+ arrlist);}catch(IllegalArgumentException e) {System.out.println("Exception thrown : "+ e);}catch(NoSuchElementException e) {System.out.println("Exception thrown : "+ e);}}}chevron_rightfilter_noneOutput:List elements before fill: [20, 30, 40] List elements after fill: [500, 500, 500]
Recommended Posts:
- Collections max() method in Java with Examples
- Collections min() method in Java with Examples
- Collections list() method in Java with Examples
- Collections indexOfSubList() method in Java with Examples
- Collections copy() method in Java with Examples
- Collections checkedSet() method in Java with Examples
- Collections checkedSortedMap() method in Java with Examples
- Collections checkedSortedSet() method in Java with Examples
- Collections enumeration() method in Java with Examples
- Collections replaceAll() method in Java with Examples
- Collections swap() method in Java with Examples
- Collections synchronizedSortedSet() method in Java with Examples
- Collections checkedMap() method in Java with Examples
- Collections singletonMap() method in Java with Examples
- Collections synchronizedSortedMap() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



