Friday, August 28, 2009

Implement the Queue in Java

A queue holds a collection of data or elements and follows the FIFO ( First In First Out) rule. The FIFO that means which data added first in the list, only that element can be removed or retrieved first from the list. In other sense, You can remove or perform operation on that data which had been added first in the Collection (list). Whenever you need to remove the last added element then you must remove all these elements which are entered before the certain element.

Using following Queue Java Program, You can do :

1. Get Size of queue
2. Get first element and don't remove it from Queue
3. Get first element and remove it from Queue
4. Check that wether element is present in Queue or not
5. Clear the queue
6. Check that Queue is cleared or not
7. Add Elements into Queue
8. Iterate and list elements in Queue


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anuj.basic;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

/**
 *
 * @author Anuj Patel
 */
public class QueueOperation {
    
    protected static Queue queue = new LinkedList();
    
    /**
     * Display Queue
     * @param queue 
     */
    private void printQueue(){        
        Iterator itr = queue.iterator();
        while(itr.hasNext()){
            System.out.println(itr.next());
        }
    }
    
    /**
     * Add Elements into Queue
     * @param queue 
     */
    private void addElements(){        
        queue.add(5);
        queue.add(1);
        queue.add(10);
        queue.add(25);
        queue.add(4);
    }
    
    /**
     * Display Size of Queue
     */
    private void getSize(){
        System.out.println("Initial Size of Queue - "+queue.size());
    }
    
    public static void main(String[] args) {
        QueueOperation queueObj = new QueueOperation();
         
        //add elements into queue
        queueObj.addElements();
        queueObj.printQueue();
        
        //get size of queue
        queueObj.getSize(); 
        
        //retrieve first element from queur but don't remove it from queue
        System.out.println("Get First Element and don't remove from Quque - "+queue.peek());
        
        //retrieve first element from queur and remove it from queue
        System.out.println("Get First Element and remove from Quque - "+queue.poll());
        
        System.out.println("Size of Queue afetr removing elements - "+queue.size());
        System.out.println("Queue after removing elements - ");
        queueObj.printQueue();
        
        //check element is present ot not in Queue
        System.out.println("Element 10 present or not - " + queue.contains(10));
        System.out.println("Element 100 present or not - " + queue.contains(100));
        
        //retrieve first element from queur but don't remove it from queue
        //This method differs from peek only in that it throws an exception if this queue is empty.
         System.out.println("Get First Element and don't remove from Quque - "+queue.element());
        
        //clear elements in queue
        queue.clear();
        System.out.println("Is Queue Cleared - "+queue.isEmpty());        
    }
}
 
Output :
run:
5
1
10
25
4
Initial Size of Queue - 5
Get First Element and don't remove from Quque - 5
Get First Element and remove from Quque - 5
Size of Queue afetr removing elements - 4
Queue after removing elements - 
1
10
25
4
Element 10 present or not - true
Element 100 present or not - false
Get First Element and don't remove from Quque - 1
Is Queue Cleared - true
BUILD SUCCESSFUL (total time: 4 seconds)
 


Author : Anuj Patel
LinkedIn : http://www.linkedin.com/in/anujjpatel
Blog : http://goldenpackagebyanuj.blogspot.in/

No comments:

Post a Comment