Saturday, 12 April 2014

peek : Retrieves the head of the queue

E peek()
This method is equivalent to peekFirst(). Retrieves, but does not remove, the head of the dequeue.

import java.util.*;

class DequePeek{
  public static void main(String args[]){
    Deque<Integer> myDeque = new LinkedList<Integer> ();
  
    /* Add Elements to myDeque */
    myDeque.add(10);
    myDeque.add(20);
    myDeque.add(30);
    myDeque.add(40);
  
    System.out.println("Elements in Deque are " + myDeque);
  
    System.out.println("\nHead Of the Deque are " + myDeque.peek());
    System.out.println("\nHead Of the Deque are " + myDeque.peek());
  }
}

Output
Elements in Deque are [10, 20, 30, 40]

Head Of the Deque are 10

Head Of the Deque are 10


1. Return null if this deque is empty
import java.util.*;

class DequePeekNull{
  public static void main(String args[]){
    Deque<Integer> myDeque = new LinkedList<Integer> ();
    
    System.out.println("Elements in Deque are " + myDeque); 
    System.out.println("\nHead Of the Deque are " + myDeque.peek());
  }
}

Output
Elements in Deque are []

Head Of the Deque are null

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment