Sunday, 3 June 2012

java List and Queue Interface

List Interface :

The List interface extends the Collection interface to define an ordered collection. It is also known as the sequence collection which permits duplicates element to accommodate in  the set but does not map a key value to an object. It permits one or more elements to be null.

This interface adds position-oriented operations such as insert an element, get an element as well as remove or change an element based on their numerical position in the list. It also performs search operation to allow searching a specified object in the list and returns its numerical position.

In addition to methods of  the Set interface, it provides following three methods shown in the table below:

get( ) Returns the element at the specified index position in this collection

listIterator( ) Returns a List Iterator object for the collection which may then be used to retrieve an object

Queue Interface:

A Queue interface extends the Collection interface to define an ordered collection for holding elements  in a FIFO (first-in-first-out) manner to process them i.e. in a FIFO queue the element that is inserted firstly will also get removed  first.

Besides basic collection operations, queues also provide additional operations such as insertion, removal, and inspection .
The Methods of this interface are as follows.

peek( ) Returns an element but if queue is empty then it returns null

 poll( )  Removes an element but returns null if the queue is empty.


implementation-classes:

ArrayList
LinkedList
RunArrayList


import java.util.*;

public class ListQueueDemo {
  public static void main(String args[]) {
  int numbers[]={34, 22,10,60,30};
 List <Integer>list = new ArrayList<Integer>();
  try{
  for(int i=0; i<5; i++){
  list.add(numbers[i]);
  }
  System.out.println("the List is: ");
  System.out.println(list);
  LinkedList <Integer>queue = new LinkedList<Integer>();
  for(int i=0; i<5; i++){
  queue.addFirst(numbers[i]);
  }
  System.out.println("The Oueue is: ");
 System.out.println(queue);
  queue.removeLast();
  System.out.println("After removing last element the queue is: "+ queue);
 
  }
  catch(Exception e){}
  }
}

No comments:

Post a Comment