Queue offer vs add in Java

A Queue
is a data structure that allows users to add elements in the First in First out
manner. In Java, a queue
is an interface present in the java.util
package. It is a linear structure that allows ordered elements in the data structure.
The collection is open from both the ends, or the rear, and the front end. The property enables objects to be added at the front and removed from the rear end.
The structure offers various methods, and its description is in the code below.
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
public class Main {
public static void main(String[] args) {
Queue queue = new ArrayBlockingQueue(2);
boolean value = false;
value = queue.offer(1);
System.out.println("Offer Returned Value: " + value);
value = queue.offer(2);
System.out.println("Offer Returned Value: " + value);
value = queue.offer(3);
System.out.println("Offer Returned Value: " + value);
Queue blockingQueue = new ArrayBlockingQueue(2);
blockingQueue.add(5);
blockingQueue.add(10);
blockingQueue.add(15);
}
}
The code block above contains both implementations of the add
and offer
method and provides the difference between both. The code block above instantiates a queue
instance using the new
keyword.
The first statement creates an object of the ArrayBlockingQueue
with the fixed capacity of size two and the default access policy. It takes a size value as a parameter. It throws an IllegalArgumentException
if the capacity is smaller than one.
The offer
method inserts the specified element into this queue after checking the capacity restrictions. The function takes the parameter as the element to be added. This function returns true
if the value is added to the end of the queue; else, false
.
It throws a ClassCastException
if the class of the specified element prevents it from getting added to it. The NullPointerException
is called if the specified value is null. The IllegalArgumentException
is thrown if some element property prevents it from being added to the queue.
The print
statement next to the offer
function prints the Boolean value true
or false
. Again, a new ArrayBlockingQueue
is instantiated with the capacity of size two
. It appends until two successful insertions and then throws an Exception
trace.
This method takes a parameter as an element to add to the queue. It returns true
each time once the successful addition is made; else, it throws an IllegalStateException
if the value cannot be added.
The ClassCastException
is thrown if the class of the specified element prevents it from addition. The NullPointerException
if the specified value is null, and IllegalArgumentException
if a property of this element prevents it from being added to the queue.
The only difference between the methods is that the offer
method throws true
or false
if the addition is made. As opposed to this, the add
method throws an exception when no more additions are possible in the queue.
Below is the resultant output from the code block above.
Offer Returned Value: true
Offer Returned Value: true
Offer Returned Value: false
Exception in thread "main" java.lang.IllegalStateException: Queue full
at java.util.AbstractQueue.add(AbstractQueue.java:98)
at java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:312)
at F11.QueueAddVsOffer.main(QueueAddVsOffer.java:25)
Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.
LinkedInRelated Article - Java Queue
- Concurrent Queue Implementation in Java
- Java Max Priority Queue
- Enqueue and Dequeue in Java
- PriorityQueue in Java
- FIFO Queue in Java