Queue: peek() : Queue « java.util « Java by API






Queue: peek()

 

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

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);
    }
  }
}

   
  








Related examples in the same category

1.Queue: add(String e)
2.Queue: element()
3.Queue: poll()
4.Queue: size()