Thursday, June 28, 2012

Queue Example in Java


Queue is a collection to store the data and follows the FIFO (First In First Out) pattern to remove the elements from it. Following is the implementation of Queue in java.

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

public class QueueDemo {
      public static void main(String[] args) {
            Queue<String> queue = new LinkedList<String>();
            for(int i=0; i<10; i++){
                  queue.offer(String.valueOf((i*7)%10));
            }
            System.out.print("\nElements present in Queue (First In First Out): ");
            for(String str: queue){
                  System.out.print(str + " ");
            }
            System.out.print("\nRemoving Elements from Queue (First In First Out):");
            while(!queue.isEmpty()){
                  System.out.print(queue.poll() + " ");
            }
      }
}

No comments:

Post a Comment