Home » Java Deque element() Method with Examples

Java Deque element() Method with Examples

by Online Tutorials Library

Java Deque element() Method

The element() method of Java Deque Interface retrieves but not remove the head of the queue which is represented by the deque. The above method differs from the peek only in one way that it throws an exception only if the deque is empty.

Specified by:

  • element in interfaceQueue<E>

Syntax:

Parameter:

N.A.

Return:

The above method returns the head of the deque represented by the given deque.

Throw:

NoSuchElementException– if the given deque is emplty.

Example 1

Test it Now

Output:

The final queue is : [FAT, CAO, JAVA, DBMS]  The required answer : FAT  

Example 2

Test it Now

Output:

The list of the elements is given as :   [k, p, m, t]  The required answer : k  

Example 3

Test it Now

Output:

The list of the elements is given as : [2, 3, 4, 5]  The required answer : 2  
Next TopicJava Deque

You may also like