Wednesday, July 3, 2024
HomeLanguagesJavaCollections list() method in Java with Examples

Collections list() method in Java with Examples

The list() method of java.util.Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that require collections.

Syntax:

public static ArrayList list(Enumeration e)

Parameters: This method takes enumeration e as a parameter providing elements for the returned array list.

Return Value: This method returns an array list containing the elements returned by the specified enumeration.

Below are the examples to illustrate the list() method

Example 1:




// Java program to demonstrate
// list() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating object of List<String>
            List<String> arrlist = new ArrayList<String>();
  
            // creating object of Vector<String>
            Vector<String> v = new Vector<String>();
  
            // Adding element to Vector v
            v.add("A");
            v.add("B");
            v.add("C");
            v.add("D");
            v.add("E");
  
            // printing the list
            System.out.println("Current list : " + arrlist);
  
            // creating Enumeration
            Enumeration<String> e = v.elements();
  
            // getting arrlist of specified Enumeration
            // using list() method
            arrlist = Collections.list(e);
  
            // printing the arrlist
            System.out.println("Returned list: " + arrlist);
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Current list : []
Returned list: [A, B, C, D, E]

Example 2:




// Java program to demonstrate
// list() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating object of List<String>
            List<Integer> arrlist = new ArrayList<Integer>();
  
            // creating object of Vector<String>
            Vector<Integer> v = new Vector<Integer>();
  
            // Adding element to Vector v
            v.add(10);
            v.add(20);
            v.add(30);
            v.add(40);
            v.add(50);
  
            // printing the list
            System.out.println("Current list : " + arrlist);
  
            // creating Enumeration
            Enumeration<Integer> e = v.elements();
  
            // getting arrlist of specified Enumeration
            // using list() method
            arrlist = Collections.list(e);
  
            // printing the arrlist
            System.out.println("Returned list: "
                               + arrlist);
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Current list : []
Returned list: [10, 20, 30, 40, 50]

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments