Share on Google+Share on Google+

Array to Collection

In this example we are converting values of an array into collection.

Array to Collection


In this example we are converting values of an array into collection.

List  interface is a member of the Java Collection Framework and extends Collection interface. List interface is an ordered collection which follows insertion order, typically allow duplicate elements. The List interface provides a special iterator, called ListIterator for insertion, replacement and  bi-directional access. 
The Arrays class contains various methods for manipulating arrays (e.g. sorting and searching etc.) and extends the Object class. The Iterator class allows access to the elements of a collection.

Methods used:
This method is used to get more elements. If the element at the next position is found then returns true otherwise throws an exception.

next(): This method returns the next element in the iteration.

asList(Object[] a): This method acts as a bridge between array and collection. This method converts array into Arrays by internally using  Colletion.toArray() method .

The code of the program is given below:

import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
public class ArraysToCollection{
  public static void main(String[] args){
  String[] array = {"Java""Struts""JSP""J2EE"};
  List list = Arrays.asList(array);
  Iterator iterator = list.iterator();
  System.out.println("\nThe values into Array");
  while (iterator.hasNext()){

The output of the program is given below:

C:\rajesh\kodejava>java ArraysToCollection
The values into Array

Download this example.



Posted on: December 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Array to Collection   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image