Queue offer() method in Java
Last Updated :
26 Sep, 2018
Improve
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:
Java
Java
Java
Java
Java
boolean offer(E e)Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the Queue. Returns: This method returns true on successful insertion else it returns false. 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.
- 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.
// Java Program Demonstrate offer()
// method of Queue
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new LinkedBlockingQueue<Integer>(3);
if (Q.offer(10))
System.out.println("The Queue is not full"
+ " and 10 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(15))
System.out.println("The Queue is not full"
+ " and 15 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(25))
System.out.println("The Queue is not full"
+ " and 25 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(20))
System.out.println("The Queue is not full"
+ " and 20 is inserted");
else
System.out.println("The Queue is full");
// before removing print Queue
System.out.println("Queue: " + Q);
}
}
Output:
Program 2: With the help of ConcurrentLinkedDeque.
The Queue is not full and 10 is inserted The Queue is not full and 15 is inserted The Queue is not full and 25 is inserted The Queue is full Queue: [10, 15, 25]
// Java Program Demonstrate offer()
// 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>();
if (Q.offer(10))
System.out.println("The Queue is not full"
+ " and 10 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(15))
System.out.println("The Queue is not full"
+ " and 15 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(25))
System.out.println("The Queue is not full"
+ " and 25 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(20))
System.out.println("The Queue is not full"
+ " and 20 is inserted");
else
System.out.println("The Queue is full");
// before removing print Queue
System.out.println("Queue: " + Q);
}
}
Output:
Program 3: With the help of ArrayDeque.
The Queue is not full and 10 is inserted The Queue is not full and 15 is inserted The Queue is not full and 25 is inserted The Queue is not full and 20 is inserted Queue: [10, 15, 25, 20]
// Java Program Demonstrate offer()
// 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>(6);
if (Q.offer(10))
System.out.println("The Queue is not full"
+ " and 10 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(15))
System.out.println("The Queue is not full"
+ " and 15 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(25))
System.out.println("The Queue is not full"
+ " and 25 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(20))
System.out.println("The Queue is not full"
+ " and 20 is inserted");
else
System.out.println("The Queue is full");
// before removing print Queue
System.out.println("Queue: " + Q);
}
}
Output:
Program 4: With the help of LinkedList.
The Queue is not full and 10 is inserted The Queue is not full and 15 is inserted The Queue is not full and 25 is inserted The Queue is not full and 20 is inserted Queue: [10, 15, 25, 20]
// Java Program Demonstrate offer()
// 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>();
if (Q.offer(10))
System.out.println("The Queue is not full"
+ " and 10 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(15))
System.out.println("The Queue is not full"
+ " and 15 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(25))
System.out.println("The Queue is not full"
+ " and 25 is inserted");
else
System.out.println("The Queue is full");
if (Q.offer(20))
System.out.println("The Queue is not full"
+ " and 20 is inserted");
else
System.out.println("The Queue is full");
// before removing print Queue
System.out.println("Queue: " + Q);
}
}
Output:
Below programs illustrate exceptions thrown by this method:
Program 5: To show NullPointerException.
The Queue is not full and 10 is inserted The Queue is not full and 15 is inserted The Queue is not full and 25 is inserted The Queue is not full and 20 is inserted Queue: [10, 15, 25, 20]
// Java Program Demonstrate offer()
// method of Queue when Null is passed
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
throws NullPointerException
{
// create object of Queue
Queue<Integer> Q
= new LinkedBlockingQueue<Integer>();
// Add numbers to end of Deque
Q.offer(7855642);
Q.offer(35658786);
Q.offer(5278367);
try {
// when null is inserted
Q.offer(null);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output:
Note: The other two exceptions are internal and are caused depending on the compiler hence cannot be shown in the code.
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#offer-E-
Exception: java.lang.NullPointerException