Open In App

Queue remove() method in Java

Last Updated : 18 Aug, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 function throws an NoSuchElementException when the Queue is empty. 

Below programs illustrate remove() method of Queue: 

Program 1: With the help of LinkedList

Java




// Java Program Demonstrate remove()
// 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 and deletes the head
        System.out.println("Queue's head: " + Q.remove());
 
        // print head and deleted the head
        System.out.println("Queue's head: " + Q.remove());
    }
}


Output:

Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue's head: 35658786

Program 2: With the help of ArrayDeque

Java




// Java Program Demonstrate remove()
// 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 and deletes the head
        System.out.println("Queue's head: " + Q.remove());
 
        // print head and deleted the head
        System.out.println("Queue's head: " + Q.remove());
    }
}


Output:

Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue's head: 35658786

Program 3: With the help of LinkedBlockingDeque

Java




// Java Program Demonstrate remove()
// 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 and deletes the head
        System.out.println("Queue's head: " + Q.remove());
 
        // print head and deleted the head
        System.out.println("Queue's head: " + Q.remove());
    }
}


Output:

Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue's head: 35658786

Program 4: With the help of ConcurrentLinkedDeque

Java




// Java Program Demonstrate remove()
// 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 and deletes the head
        System.out.println("Queue's head: " + Q.remove());
 
        // print head and deleted the head
        System.out.println("Queue's head: " + Q.remove());
    }
}


Output:

Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue's head: 35658786

Below programs illustrate exceptions thrown by this method: Program 5: To show NoSuchElementException

Java




// Java Program Demonstrate remove()
// 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(423);
        Q.add(3432);
 
        // print queue
        System.out.println("Queue: " + Q);
 
        // print head and deletes the head
        System.out.println("Queue's head: " + Q.remove());
 
        // print head and deleted the head
        System.out.println("Queue's head: " + Q.remove());
 
        // 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: [423, 3432]
Queue's head: 423
Queue's head: 3432
Queue: []
Exception: java.util.NoSuchElementException

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#remove–



Similar Reads

Difference Between poll() and remove() method of Queue Interface in Java
The queue is a data structure that stores elements in a first-in, first-out (FIFO) order. It can be used to implement a priority queue or as a basic synchronization primitive. The remove() method removes an element from the head of this queue and returns it. The poll() method blocks until one or more elements become available for removal, then retu
4 min read
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: boolean add(E e) Parameters: This method accepts a manda
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 front of the container or the head of the Queue. I
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 method returns the head of the Queue, it returns fa
3 min read
Queue element() method in Java
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. Excep
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 since it returns false. Syntax: boolean offer(E e)
5 min read
Priority Queue in Reverse Order in Java
A PriorityQueue is used when the objects are supposed to be processed based on the priority. It is known that a Queue follows the First-In-First-Out algorithm, but sometimes the elements of the queue are needed to be processed according to the priority, that’s when the PriorityQueue comes into play. The PriorityQueue is based on the priority heap.
4 min read
How to implement Stack and Queue using ArrayDeque in Java
ArrayDeque in Java The ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both sides of the queue. ArrayDeque class implements Queu
6 min read
How to implement size-limited Queue that holds last N elements in Java?
What is Size Limited Queue? Size limited queue is the same as a normal queue but it has the special property that at any time queue can store at max N element, if we try to push more than N elements in the queue then accordingly the elements added at the starting will be removed from the queue means size will remain to fix i.e. N. If you want to kn
3 min read
Difference between add() and offer() methods in Queue in Java
Add() and Offer() are the methods used for adding the elements in the Queue. But both have their main function and they treat the elements differently. add() method in Java: It Inserts the specified element to the end of the queue if there is space, returning true upon success and throwing an IllegalStateException if no space is currently available
4 min read
Java Program to Implement the Queue Data Structure
Queue is the fundamental data structure that follows the First-In-First-Out(FIFO) principle where the element that is inserted first is one that gets removed first. Imagine the queue of the people waiting in the line at the ticket counter: the person who arrives the first gets served first and so on. Organization of the Queue Data StructureThe queu
4 min read
Queue Implementation Using Linked List in Java
Queue is the linear data structure that follows the First In First Out(FIFO) principle where the elements are added at the one end, called the rear, and removed from the other end, called the front. Using the linked list to implement the queue allows for dynamic memory utilization, avoiding the constraints of the fixed-size data structure like an a
4 min read
Queue Interface In Java
Let us start with a simple Java code snippet demonstrating creating a Queue Interface in Java. [GFGTABS] Java import java.util.LinkedList; import java.util.Queue; public class QueueCreation { public static void main(String args[]) { // Create a Queue of Integers using LinkedList Queue<Integer> queue = new LinkedList<>(); // Displaying t
13 min read
TreeSet remove() Method in Java
The Java.util.TreeSet.remove(Object O) method is to remove a particular element from a Tree set. Syntax: TreeSet.remove(Object O) Parameters: The parameter O is of the type of Tree set and specifies the element to be removed from the set. Return Value: This method returns True if the element specified in the parameter is initially present in the Se
1 min read
EnumMap remove() Method in Java
The Java.util.EnumMap.remove(key) method in Java is used to remove the specified key from the map. Syntax: remove(Object key) Parameters: The method takes one parameter key which refers to the key whose mapping is to be removed. Return Value: The method does not return any value. Below programs illustrate the working of remove(key) function: Progra
2 min read
LinkedList remove() Method in Java
LinkedList as we all know is a way of storing data that contains sets of nodes where each node contains data and address part where address part is responsible for linking of nodes and hence forming a List over which now we can perform operations. Now here we want to remove a node/s using the remove() method of LinkedList class only. Illustration:
4 min read
HashSet remove() Method in Java
HashSet remove() method is used to remove a particular element from a HashSet. Note that it is only after JDK version 1.2 and ahead, and will throw compilation errors before in version JDK 1 and JDK1.1. Note: This method returns true if the specified element is present in the HashSet otherwise it returns boolean false. Syntax: HashSet.remove(Object
2 min read
PriorityQueue remove() Method in Java
The remove() method of PriorityQueue class of java.util package is used to remove a particular element from a PriorityQueue. As we all know that the elements while entering into the priority queue are not sorted but as we all know while taking out elements from the priority queue the elements are always sorted being a trait of the priority queue. H
5 min read
HashMap remove() Method in Java
The java.util.HashMap.remove() is an inbuilt method of HashMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map.Syntax: Hash_Map.remove(Object key) Parameters: The method takes one parameter key whose mapping is to be removed from the Map.Return Value: The
2 min read
TreeMap remove() Method in Java
The java.util.TreeMap.remove() is an inbuilt method of TreeMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map. Syntax: Tree_Map.remove(Object key) Parameters: The method takes one parameter key whose mapping is to be removed from the Map. Return Value: Th
2 min read
ArrayDeque remove() Method in Java
The Java.util.ArrayDeque.remove() method is used to remove the element present at the head of the Deque. Syntax: Array_Deque.remove() Parameters: The method does not take any parameters. Return Value: This method returns the element present at the head of the Deque. Exceptions: The method throws NoSuchElementException is thrown if the deque is empt
3 min read
WeakHashMap remove() method in Java
The java.util.WeakHashMap.remove() is an inbuilt method of WeakHashMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map. Syntax: Weak_Hash_Map.remove(Object key) Parameters: The method takes one parameter key whose mapping is to be removed from the Map. Ret
2 min read
ArrayBlockingQueue remove() method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue also follows FIFO (first-in-first-out) rule for st
3 min read
IdentityHashMap remove() Method in Java
The java.util.IdentityHashMap.remove() is an inbuilt method of IdentityHashMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map. Syntax: Identity_Hash_Map.remove(Object key) Parameters: The method takes one parameter key whose mapping is to be removed from
2 min read
Vector remove() Method in Java
remove(int index) The java.util.vector.remove(int index) method is used to remove an element from a Vector from a specific position or index. Syntax: Vector.remove(int index) Parameters: This method accepts a mandatory parameter index is of integer data type and specifies the position of the element to be removed from the Vector. Return Value: This
2 min read
LinkedBlockingQueue remove() method in Java
The remove(Object obj) method of LinkedBlockingQueue removes only one instance of the given Object, passed as parameter, from this LinkedBlockingQueue if it is present. It removes an element e such that obj.equals(e) and if this queue contains one or more instance of element e. This method returns true if this queue contained the element which is n
4 min read
PriorityBlockingQueue remove() method in Java
The remove(Object o) method of PriorityBlockingQueue is used to delete an element from this queue. This method removes a single instance of the element passed as the parameter, if it is present. It returns true if and only if the element was removed, else it returned false. Syntax: public boolean remove(Object o) Parameter: This method accepts a ma
2 min read
ConcurrentHashMap remove() method in Java
The remove() method in Java's ConcurrentHashMap class is used to remove the entry for a given key from the map. It has the following signature: V remove(Object key)where: key is the key of the entry to be removed.V is the type of values in the map.The remove() method works in a concurrent environment, which means that multiple threads can access an
7 min read
LinkedTransferQueue remove() method in Java
The java.util.concurrent.LinkedTransferQueue.remove() method is an in-built function in Java which is used to remove an element if it is present in this queue. Syntax: LinkedTransferQueue.remove(Object o) Parameters: The function accepts a single parameter o i.e. the object to be removed. Return Value: The function returns a true boolean value on s
2 min read
ConcurrentSkipListSet remove() method in Java
The java.util.concurrent.ConcurrentSkipListSet.remove() method is an in-built function in Java which is used to remove an element if it is present in this set. Syntax: ConcurrentSkipListSet.remove(Object o) Parameters: The function accepts a single parameter o i.e. the object to be removed. Return Value: The function returns a true boolean value on
2 min read
three90RightbarBannerImg