By candid | Posted :
Jun 26, 2015
| Updated :
Jun 26, 2015
Retrieves, but does not remove, the head (first element) of this list.
Program
package com.candidjava;
import java.util.LinkedList;
/**
*
* @author : mohan
* @description :The java.util.LinkedList.peek() method retrieves, but does not
* remove, the head (first element) of this list.
*/
public class LinkedListPeek {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add("anandh");
list.add("hari");
list.add("vinoth");
list.add("mohan");
list.add("kamal");
System.out.println("LinkedList:" + list);
System.out.println("Head of the list:" + list.peek());
}
}
Output
LinkedList:[anandh, hari, vinoth, mohan, kamal]
Head of the list:anandh
Explanation
public E peek()
Retrieves, but does not remove, the head (first element) of this list.
Specified by:
peek in interface Deque<E>
Specified by:
peek in interface Queue<E>
Returns:
the head of this list, or null if this list is empty