Queue element() method in Java Last Updated : 26 Sep, 2018 Comments Improve Suggest changes Like Article Like Report The element() method of Queue Interface returns the element at the front the container. It does not deletes the element in the container. This method returns the head of the queue. This method differs from peek() only in that it throws an exception if this queue is empty. Syntax: E element() Returns: This method returns the head of the Queue. Exception: The function throws NoSuchElementException when the queue is empty and the function is called. Below programs illustrate element() method of Queue: Program 1: With the help of LinkedList. Java // Java Program Demonstrate element() // method of Queue import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Queue Queue<Integer> Q = new LinkedList<Integer>(); // Add numbers to end of Queue Q.add(7855642); Q.add(35658786); Q.add(5278367); Q.add(74381793); // print queue System.out.println("Queue: " + Q); // print head System.out.println("Queue's head: " + Q.element()); } } Output: Queue: [7855642, 35658786, 5278367, 74381793] Queue's head: 7855642 Program 2: With the help of ArrayDeque. Java // Java Program Demonstrate element() // method of Queue import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Queue Queue<Integer> Q = new ArrayDeque<Integer>(); // Add numbers to end of Queue Q.add(7855642); Q.add(35658786); Q.add(5278367); Q.add(74381793); // print queue System.out.println("Queue: " + Q); // print head System.out.println("Queue's head: " + Q.element()); } } Output: Queue: [7855642, 35658786, 5278367, 74381793] Queue's head: 7855642 Program 3: With the help of LinkedBlockingDeque. Java // Java Program Demonstrate element() // method of Queue import java.util.*; import java.util.concurrent.LinkedBlockingDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Queue Queue<Integer> Q = new LinkedBlockingDeque<Integer>(); // Add numbers to end of Queue Q.add(7855642); Q.add(35658786); Q.add(5278367); Q.add(74381793); // print queue System.out.println("Queue: " + Q); // print head System.out.println("Queue's head: " + Q.element()); } } Output: Queue: [7855642, 35658786, 5278367, 74381793] Queue's head: 7855642 Program 4: With the help of ConcurrentLinkedDeque. Java // Java Program Demonstrate element() // method of Queue import java.util.*; import java.util.concurrent.ConcurrentLinkedDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Queue Queue<Integer> Q = new ConcurrentLinkedDeque<Integer>(); // Add numbers to end of Queue Q.add(7855642); Q.add(35658786); Q.add(5278367); Q.add(74381793); // print queue System.out.println("Queue: " + Q); // print head System.out.println("Queue's head: " + Q.element()); } } Output: Queue: [7855642, 35658786, 5278367, 74381793] Queue's head: 7855642 Below programs illustrate exceptions thrown by this method: Program 5: To show NoSuchElementException. Java // Java Program Demonstrate element() // method of Queue import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Queue Queue<Integer> Q = new LinkedList<Integer>(); // Add numbers to end of Queue Q.add(7855642); Q.add(35658786); Q.add(5278367); Q.add(74381793); // print queue System.out.println("Queue: " + Q); // print head System.out.println("Queue's head: " + Q.element()); Q.clear(); // print queue System.out.println("Queue: " + Q); try { // Queue is empty now hence exception System.out.println("Queue's head: " + Q.element()); } catch (Exception e) { System.out.println("Exception: " + e); } } } Output: Queue: [7855642, 35658786, 5278367, 74381793] Queue's head: 7855642 Queue: [] Exception: java.util.NoSuchElementException Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#element-- Comment More infoAdvertise with us Next Article Queue element() method in Java gopaldave Follow Improve Article Tags : Misc Java Java-Collections Java - util package java-basics Java-Functions java-queue +3 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Queue add() method in Java The add(E e) method of Queue Interface inserts the element passed in the parameter to the end of the queue if there is space. If the Queue is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax: boolea 4 min read Queue poll() method in Java The poll() method of Queue Interface returns and removes the element at the front end of the container. It deletes the element in the container. The method does not throws an exception when the Queue is empty, it returns null instead. Syntax: E poll() Returns: This method returns the element at the 3 min read Queue peek() method in Java The peek() method of Queue Interface returns the element at the front the container. It does not deletes the element in the container. This method returns the head of the queue. The method does not throws an exception when the Queue is empty, it returns null instead. Syntax: E peek() Returns: This m 3 min read Queue offer() method in Java The offer(E e) method of Queue Interface inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. This method is preferable to add() method since this method does not throws an exception when the capacity of the container is full s 5 min read PriorityQueue clear() Method in Java The Java.util.PriorityQueue.clear() method is used to remove all the elements from a PriorityQueue. Using the clear() method only clears all the element from the queue and does not delete the queue. In other words, we can say that the clear() method is used to only empty an existing PriorityQueue. S 2 min read Queue remove() method in Java The remove() method of Queue Interface returns and removes the element at the front of the container. It deletes the head of the container. The method throws an NoSuchElementException when the Queue is empty. Syntax: E remove() Returns: This method returns the head of the Queue. Exception: The funct 3 min read DelayQueue remove() method in Java The remove() method of DelayQueue class in Java is used to remove a single instance of the given object say obj from this DelayQueue if it is present. It returns true if the given element is removed successfully otherwise it returns false. Syntax: public boolean remove(Object obj) Parameters: This m 2 min read PriorityQueue add() Method in Java The Java.util.PriorityQueue.add() method in Java is used to add a specific element into a PriorityQueue. This method internally just calls the Java.util.PriorityQueue.offer() method with the value passed to it. So, it exactly works like offer() method. Syntax: Priority_Queue.add(Object element) Para 2 min read PriorityQueue peek() Method in Java The java.util.PriorityQueue.peek() method in Java is used to retrieve or fetch the first element of the Queue or the element present at the head of the Queue. The element retrieved does not get deleted or removed from the Queue. Syntax: Priority_Queue.peek() Parameters: The method does not take any 2 min read PriorityQueue poll() Method in Java The java.util.PriorityQueue.poll() method in Java is used to retrieve or fetch and remove the first element of the Queue or the element present at the head of the Queue. The peek() method only retrieved the element at the head but the poll() also removes the element along with the retrieval. It retu 2 min read Like