Peek a queue

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

public class Main {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<String>();

        queue.add("A");
        queue.add("B");

        queue.offer("C");
        queue.offer("D");

        System.out.println("remove: " + queue.remove());

        System.out.println("element: " + queue.element());

        System.out.println("poll: " + queue.poll());

        System.out.println("peek: " + queue.peek());
    }
}
  

Output:


remove: A
element: B
poll: B
peek: C
Home 
  Java Book 
    Runnable examples  

Collection Queue:
  1. Create a queue from LinkedList
  2. Peek a queue
  3. Poll to remove the first item from the queue