Queue add() method in Java
Last Updated :
19 Jul, 2022
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 mandatory parameter e which is the element to be inserted in the end of the Queue. Returns: This method returns true on successful insertion. Exceptions: The function throws four exceptions which are described as below:
- ClassCastException: when the class of the element to be entered prevents it from being added to this container.
- IllegalStateException: when the capacity of the container is full and insertion is done.
- IllegalArgumentException: when some property of the element prevents it to be added to the queue.
- NullPointerException: when the element to be inserted is passed as null and the Queue’s interface does not allow null elements.
Below programs illustrate add() method of Queue: Program 1: With the help of LinkedList.
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Queue<Integer> Q
= new LinkedList<Integer>();
Q.add( 7855642 );
Q.add( 35658786 );
Q.add( 5278367 );
Q.add( 74381793 );
System.out.println("Queue: " + Q);
}
}
|
Output:
Queue: [7855642, 35658786, 5278367, 74381793]
Program 2: With the help of ArrayDeque.
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Queue<Integer> Q
= new ArrayDeque<Integer>();
Q.add( 7855642 );
Q.add( 35658786 );
Q.add( 5278367 );
Q.add( 74381793 );
System.out.println("Queue: " + Q);
}
}
|
Output:
Queue: [7855642, 35658786, 5278367, 74381793]
Program 3: With the help of LinkedBlockingDeque.
Java
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Queue<Integer> Q
= new LinkedBlockingDeque<Integer>();
Q.add( 7855642 );
Q.add( 35658786 );
Q.add( 5278367 );
Q.add( 74381793 );
System.out.println("Queue: " + Q);
}
}
|
Output:
Queue: [7855642, 35658786, 5278367, 74381793]
Program 4: With the help of ConcurrentLinkedDeque.
Java
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Queue<Integer> Q
= new ConcurrentLinkedDeque<Integer>();
Q.add( 7855642 );
Q.add( 35658786 );
Q.add( 5278367 );
Q.add( 74381793 );
System.out.println("Queue: " + Q);
}
}
|
Output:
Queue: [7855642, 35658786, 5278367, 74381793]
Below programs illustrate exceptions thrown by this method: Program 5: To show NullPointerException.
Java
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Queue<Integer> Q
= new LinkedBlockingQueue<Integer>();
Q.add( 7855642 );
Q.add( 35658786 );
Q.add( 5278367 );
try {
Q.add( null );
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
|
Output:
Exception: java.lang.NullPointerException
Program 6: To show IllegalStateException.
Java
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Queue<Integer> Q
= new LinkedBlockingQueue<Integer>( 3 );
Q.add( 7855642 );
Q.add( 35658786 );
Q.add( 5278367 );
try {
Q.add( 10 );
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
|
Output:
Exception: java.lang.IllegalStateException: Queue full
Time Complexity : O(1)
Note: The other two exceptions are internal and are caused depending on the compiler hence cannot be shown in the compiler. Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#add-E-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...