Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Array to Collection

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

Ads

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:
hasNext():
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()){
   System.out.println((Stringiterator.next());
 }
 }
}

The output of the program is given below:

C:\rajesh\kodejava>javac ArraysToCollection.java
C:\rajesh\kodejava>java ArraysToCollection
The values into Array
Java
Struts
JSP
J2EE

Download this example.

Advertisements

Ads
Share on Google+Share on Google+

Array to Collection

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

Advertisements

 

Discuss: Array to Collection   View All Comments

Post your Comment


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

Ads

 

Ads