Java Collections list() Method



Description

The Java Collections list(Enumeration<T>) method is used to get an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.

Declaration

Following is the declaration for java.util.Collections.list() method.

public static <T> ArrayList<T> list(Enumeration<T> e)

Parameters

e − This is the enumeration providing elements for the returned array list.

Return Value

The method call returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.

Exception

NA

Getting List From a Vector of Integers Example

The following example shows the usage of Java Collection list(Enumeration) method. We've created a vector object, populated it with some integers. Then its enumeration is retrieved and using list(Enumeration) method an array list is prepared and printed.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create vector and array list
      List<Integer> arrlist = new ArrayList<>();
      Vector<Integer> v = new Vector<>();

      // populate the vector
      v.add(1);       
      v.add(2);
      v.add(3);
      v.add(4);
      v.add(5);

      // create enumeration
      Enumeration<Integer> e = v.elements();

      // get the list
      arrlist = Collections.list(e);

      System.out.println("Value of returned list: "+arrlist);
   }    
}  

Output

Let us compile and run the above program, this will produce the following result −

Value of returned list: [1, 2, 3, 4, 5]

Getting List From a Vector of Strings Example

The following example shows the usage of Java Collection list(Enumeration) method. We've created a vector object, populated it with some strings. Then its enumeration is retrieved and using list(Enumeration) method an array list is prepared and printed.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create vector and array list
      List<String> arrlist = new ArrayList<>();
      Vector<String> v = new Vector<>();

      // populate the vector
      v.add("A");       
      v.add("B");
      v.add("C");
      v.add("D");
      v.add("E");

      // create enumeration
      Enumeration<String> e = v.elements();

      // get the list
      arrlist = Collections.list(e);

      System.out.println("Value of returned list: "+arrlist);
   }    
}  

Output

Let us compile and run the above program, this will produce the following result −

Value of returned list: [A, B, C, D, E]

Getting List From a Vector of Objects Example

The following example shows the usage of Java Collection list(Enumeration) method. We've created a vector object, populated it with some Student objects. Then its enumeration is retrieved and using list(Enumeration) method an array list is prepared and printed.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create vector and array list
      List<Student> arrlist = new ArrayList<>();
      Vector<Student> v = new Vector<>();

      // populate the vector
      v.add(new Student(1, "Julie"));
      v.add(new Student(2, "Robert"));
      v.add(new Student(3, "Adam"));
      v.add(new Student(4, "Jene"));
      v.add(new Student(5, "Joe"));

      // create enumeration
      Enumeration<Student> e = v.elements();

      // get the list
      arrlist = Collections.list(e);

      System.out.println("Value of returned list: "+arrlist);
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Value of returned list: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 4, Jene ], [ 5, Joe ]]
java_util_collections.htm
Advertisements