Java Collections Framework- Queue Interface

Java Collections Framework- Queue Interface


Posted in : Java Posted on : November 30, 2010 at 5:56 PM Comments : [ 0 ]

Queue interface is in java.util package of java.Queue is a linear collection that supports element insertion and removal at both ends.

Java Collections Framework- Queue Interface

Queue interface is in java.util package of java.Queue is a linear collection that supports element insertion and removal at both ends.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. Queue does not require any fix dimension like String array and int array.
Besides basic collection operations, queues also provide additional operations such as insertion, removal, and inspection .

The Methods of this interface are as follows.

Here is an example that demonstrates a Queue: QueueMethodsExample.java  

In this example we use the LinkedList class which implements the Queue interface and add items to it using both the add() and offer() methods.

package devmanuals.com;
import java.util.*;
public class QueueMethodsExample {
	public static void main(String[] args) {

        Queue oqueue=new LinkedList();

        oqueue.add("1");
        oqueue.add("2");
        oqueue.add("3");
        oqueue.add("4");
        oqueue.add("5");

        Iterator itr=oqueue.iterator();

        System.out.println("Initial Size of Queue :"+oqueue.size());

        while(itr.hasNext())
        {
            String iteratorValue=(String)itr.next();
            System.out.println("Queue Next Value :"+iteratorValue);
        }

        // get value and does not remove element from queue
        System.out.println("Queue peek :"+oqueue.peek());

        // get first value and remove that object from queue
        System.out.println("Queue poll :"+oqueue.poll());

        System.out.println("Final Size of Queue :"+oqueue.size());
    }
}

Output:

Initial Size of Queue :5

 Queue Next Value :1 

Queue Next Value :2 

Queue Next Value :3 

Queue Next Value :4 

Queue Next Value :5 

Queue peek :1 

Queue poll :1 

Final Size of Queue :4

 Download This Code

There is one more Example for the implementation of the methods of this interface.

Example : QueueElementMethodExample.java

package devmanuals.com;

import java.util.*;

public class QueueElementMethodExample {
	public static void main(String[] args) {
		Queue queue = new LinkedList();
		queue.offer("11");
		queue.offer("22");
		queue.offer("33");
		queue.offer("44");

		System.out.println("The size of the queue    : " + queue.size());
		System.out.println("Queue head using peek    : " + queue.peek());
		System.out.println("Queue head using element : " + queue.element());
		System.out.println("The element of queue are : " + queue);

		Object element;
		while ((element = queue.poll()) != null) {

			System.out.println("The " + element + " is removed");

		}
		System.out.println("The Queue after removal : " + element);

	}
}

Output:-

The size of the queue : 4 

Queue head using peek : 11

 Queue head using element : 11 

The element of queue are : [11, 22, 33, 44] 

The 11 is removed 

The 22 is removed

 The 33 is removed

 The 44 is removed 

The Queue after removal : null

 

Download This Code.

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics