Java Collection Tutorial - Java Queue.element()








Syntax

Queue.element() has the following syntax.

E element()

Example

In the following code shows how to use Queue.element() method.

import java.util.LinkedList;
import java.util.Queue;
//from   w ww  . j a v a  2s  .c  o m
public class Main {
  public static void main(String[] args) {
    Queue<String> queue = new LinkedList<String>();
    queue.offer("First");
    queue.offer("Second");
    queue.offer("Third");
    queue.offer("Fourth");

    System.out.println("Size: " + queue.size());

    System.out.println("Queue head using peek   : " + queue.peek());
    System.out.println("Queue head using element: " + queue.element());

    Object data;
    while ((data = queue.poll()) != null) {
      System.out.println(data);
    }
  }
}

The code above generates the following result.