Introduction to List and Queue Interface

The List interface extends the Collection interface to define an ordered collection.

Introduction to List and Queue Interface

The List interface extends the Collection interface to define an ordered collection.

Introduction to List and Queue Interface

Introduction to 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:

Method

Uses

 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

 remove( )

Removes the element at the specified index position in this collection

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.

Method

Uses

 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.

In the Java platform, Collections Framework provides three general-purpose List and Queue implementation-classes:

ArrayList
LinkedList
RunArrayList

The following program demonstrates the implementation of List and Queue interfaces.

import java.util.*;

public class ListQueueDemo {
  public static void main(String args[]) { 
  int numbers[]={3422,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){}
  }
}


Output of the Program:

C:\nisha>javac ListQueueDemo.java

C:\nisha>java ListQueueDemo
the List is:
[34, 22, 10, 60, 30]
The Oueue is:
[30, 60, 10, 22, 34]
After removing last element the queue is: 
[30, 60, 10, 22]

C:\nisha>

This program creates a List and Queue, and inserts a group of numbers in these lists. The program than prints out the list of numbers in the set. The addFirst( ) and removeLast( ) methods add and remove an element in a FIFO manner, i.e. the first element of the queue is removed first .

Download this Program: