Speed up development with full-stack environments for every branch.

Learn More

How to use Queue in Java [collections]

7631 Runs 140768 Views 27193 Copies


mirang 126

published 4 years ago

Queue collection implements First-In Fist-Out behavior on items stored within it.
import java.util.*;
public class QueueDemo {
  static String newLine = System.getProperty("line.separator");
  public static void main(String[] args) {
    System.out.println(newLine + "Queue in Java" + newLine);
    System.out.println("-----------------------" + newLine);
    System.out.println("Adding items to the Queue" + newLine);
    //Creating queue would require you to create instannce of LinkedList and assign 
    //it to Queue
    //Object. You cannot create an instance of Queue as it is abstract
    Queue queue = new LinkedList();
    //you add elements to queue using add method

    System.out.println(newLine + "Items in the queue..." + queue + newLine);

    //You remove element from the queue using .remove method
    //This would remove the first element added to the queue, here Java
    System.out.println("remove element: " + queue.remove() + newLine);
    //.element() returns the current element in the queue, here when "java" is removed
    //the next most top element is .NET, so .NET would be printed.
    System.out.println("retrieve element: " + queue.element() + newLine);
    //.poll() method retrieves and removes the head of this queue
    //or return null if this queue is empty. Here .NET would be printed and then would 
    //be removed
    //from the queue
    System.out.println("remove and retrieve element, null if empty: " + queue.poll() + 
    //.peek() just returns the current element in the queue, null if empty
    //Here it will print Javascript as .NET is removed above
    System.out.println("retrieve element, null is empty " + queue.peek() + newLine);

Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).