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

Queue in java

In this section we will discuss about queue in java. Queue is a interface in java.util package of java.


How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value

Queue in java

In this section we will discuss about queue in java. Queue is a interface in java.util package of java. It holds the collection of data or element and follow the FIFO (first in first out) manner, which means the data which is added first into the queue will be removed first from the queue. Whatever the ordering is, head of the queue is removed first from the queue by calling remove() method. All the new element are inserted at the tail of the queue. The remove() and poll() method of queue, remove and return the head of the queue. remove() and poll() method are same in behavior but remove() will throw an exception when queue is empty and poll() will return null if queue is empty. Some of the methods as follows:

Methods Summary
element()Retrieves but does not remove, the head of the queue
peek()Retrieves but does not remove the head of this queue and return null if the queue is empty
poll()Remove the head of this queue and return null if the queue is empty
remove()Retrieves and remove the head of this queue.

Example : A Program to implement  queue

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
public class QueueDemo 
   public static void main(String[] args) {
        Queue qe=new LinkedList();

        Iterator it=qe.iterator();

        System.out.println("Initial Capacity of Queue :"+qe.size());

            String iteratorValue=(String);
        }//retrieves but does not remove from the queue
         System.out.println("peek method:"+qe.peek());
        // get first value and remove that object from queue
        System.out.println("poll method:"+qe.poll());

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

Description : In the above example we have implemented queue with subclass LinkedList and with use of peek(), poll()  method retrieving and removing the element from the queue.

Output : After compiling and executing the above program

Download SourceCode


Share on Google+Share on Google+

Queue in java

Posted on: June 7, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Queue in java  

Post your Comment

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