Monday, 18 November 2019

Que Example

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

public class QueueExmaple {

    public static void main(String[] args) {
        Queue<String> queue= new LinkedList<>();
        // Linked list implements que interface        queue.add("one");
        queue.add("two");

       System.out.println(queue.peek());// Returns head. if head is not present then returns null     
      System.out.println(queue.poll()); // Returns and Removes head
       System.out.println(queue.poll()); // Returns and Removes head 
       System.out.println(queue.peek()); // Returns head. if head is not present then returns null 
       System.out.println(queue.element()); // Returns head. 
    If head is not present then throws NoSuchElementException
    }
}

No comments:

Post a Comment