LinkedList peek element
In this chapter you will learn:
Retrieve but not remove
LinkedList
can act as Queue
or Stack
.
The peek
methods are for
Queue
or Stack
style operation.
E element()
Retrieves, but does not remove, the first element.E peek()
Retrieves, but does not remove, the first element.E peekFirst()
Retrieves, but does not remove, the first element, or returns null if this list is empty.E peekLast()
Retrieves, but does not remove, the last element, or returns null if this list is empty.
import java.util.LinkedList;
import java.util.ListIterator;
//from j a v a2 s . com
public class Main{
public static void main(String args[]) {
LinkedList<String> ll = new LinkedList<String>();
ll.add("A");
ll.add("ja v a2s.com");
ll.add("B");
ll.add("C");
System.out.println(ll.element());
System.out.println(ll.peek());
System.out.println(ll.peekFirst());
System.out.println(ll.peekLast());
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Collections