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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // fill() method
    // for String value
      
    import java.util.*;
      
    public class GFG1 {
        public static void main(String[] argv) throws Exception
        {
            try {
      
                // creating object of List<Integer>
                List<String> arrlist = new ArrayList<String>();
      
                // Adding element to srclst
                arrlist.add("A");
                arrlist.add("B");
                arrlist.add("C");
      
                // print the elements
                System.out.println("List elements before fill: "
                                   + arrlist);
      
                // fill the list
                Collections.fill(arrlist, "TAJMAHAL");
      
                // print the elements
                System.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_right

    
    

    Output:

    List elements before fill: [A, B, C]
    
    List elements after fill: [TAJMAHAL, TAJMAHAL, TAJMAHAL]
    

    Example 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // fill() method
    // for Integer value
      
    import java.util.*;
      
    public class GFG1 {
        public static void main(String[] argv) throws Exception
        {
            try {
      
                // creating object of List<Integer>
                List<Integer> arrlist = new ArrayList<Integer>();
      
                // Adding element to srclst
                arrlist.add(20);
                arrlist.add(30);
                arrlist.add(40);
      
                // print the elements
                System.out.println("List elements before fill: "
                                   + arrlist);
      
                // fill the list
                Collections.fill(arrlist, 500);
      
                // print the elements
                System.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_right

    
    

    Output:

    List elements before fill: [20, 30, 40]
    
    List elements after fill: [500, 500, 500]
    


    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    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