Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Introduction to List and Queue Interface 
 

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

 

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:

                         

» View all related tutorials
Related Tags: c algorithm api ide collections orm ant data form interface framework software io multiple ip order vi collection tag int

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.