Search This Blog

Thursday 14 August 2014

Queue - JAVA

Queue in Java

Queue is First in First out. Item inserted first comes out first when removed like a queue.



This program shows how to use Queue in Java.

Queue.java

import java.util.LinkedList;

public class MainClass {
 public static void main(String args[]){
  LinkedList linkedList = new LinkedList();
  linkedList.add("Item 1");
  linkedList.add("Item 2");
  linkedList.add("Item 3");
  System.out.println(linkedList); //Items are added in FIFO.
  
  linkedList.remove();
  System.out.println(linkedList); // First inserted has taken out first
  
  //Simply get without removing from queue
  String item = linkedList.element();
  System.out.println("Item at top: " + item);
 }
}

No comments:

Post a Comment