Home » Java programming language

Java Collections list() Method with Example

Collections Class list() method: Here, we are going to learn about the list() method of Collections Class with its syntax and example.
Submitted by Preeti Jain, on February 04, 2020

Collections Class list() method

  • list() method is available in java.util package.
  • list() method is used to return an array list that contains all the elements returned by the given Enumeration and the way of storing the elements in an ArrayList in the order as returned by the enumeration.
  • list() method is a static method, so it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • list() method does not throw an exception at the time of conversion of the given Enumeration to an Arraylist.

Syntax:

    public static Arraylist list(Enumeration en);

Parameter(s):

  • Enumeration en – represents the Enumeration that pass all the elements for the returned ArrayList.

Return value:

The return type of this method is ArrayList, it returns an ArrayList of the given Enumeration.

Example:

// Java program is to demonstrate the example
// of ArrayList list() of Collections

import java.util.*;

public class ListOfCollections {
    public static void main(String args[]) {
        // Instantiate an ArrayList and 
        // Stack object
        List arr_l = new ArrayList();
        Stack st = new Stack();

        // By using push() method is
        // to add elements in stack
        st.push(10);
        st.push(20);
        st.push(30);
        st.push(40);
        st.push(50);

        // Get elements in an enumeration object
        Enumeration en = st.elements();

        // By using list() method is to 
        // return the array list of the 
        // given enumeration object
        arr_l = Collections.list(en);

        System.out.println("Collections.list(en): " + arr_l);
    }
}

Output

Collections.list(en): [10, 20, 30, 40, 50]



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.