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. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false.
Exceptions: This method throws 5 exceptions listed below as follows:
- UnsupportedOperationException: if the add operation is not supported by this collection
- ClassCastException: if the class of the specified element prevents it from being added to this collection
- NullPointerException: if the specified element is null and this collection does not permit null elements
- IllegalArgumentException: if some property of the element prevents it from being added to this collection
- IllegalStateException: if the element cannot be added at this time due to insertion restrictions
Below is the Implementation of add() method in java:
Java
// add() method: import java.util.*; public class Main { public static void main(String[] args) { // Creating Queue using the LinkedList Queue<Integer> numbers = new LinkedList<>(); // Insert element at the rear of the queue // using add method numbers.add( 1 ); numbers.add( 2 ); numbers.add( 3 ); System.out.println( "Queue using add method: " + numbers); } } |
Queue using add method: [1, 2, 3]
If the element cannot be added at this time due to insertion restrictions:
Java
import java.util.*; class GFG { public static void main(String[] args) { ArrayList<String> nums = new ArrayList<String>(); // Adding Elements nums.add( "CS" ); nums.add( "Mech" ); nums.add( "Electrical" ); Iterator<String> itr = nums.iterator(); itr.remove(); } } |
Output: Exception in thread "main" java.lang.IllegalStateException at java.base/java.util.ArrayList$Itr.remove(ArrayList.java:1010) at GFG.main(GFG.java:13)
Offer() method in Java:
It Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add(E), which can fail to insert an element only by throwing an exception.
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 three exceptions which are described 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.
Below is the Implementation of offer() method in java:
Java
import java.util.*; public class Main { public static void main(String[] args) { // Creating Queue using the LinkedList Queue<Integer> numbers = new LinkedList<>(); // Insert element at the rear // of the queue using offer numbers.offer( 1 ); numbers.offer( 2 ); numbers.offer( 3 ); numbers.offer( 4 ); System.out.print( "Queue using offer method: " + numbers); } } |
Queue using offer method: [1, 2, 3, 4]
Java
import java.util.*; class GFG { public static void main(String[] args) { // Declaration for priority Queue Queue<String> str = new PriorityQueue<String>(); // Adding Elements str.offer( "CS" ); str.offer( "Mechanical" ); str.offer( "Electrical" ); str.offer( "Civil" ); System.out.print(str); } } |
[CS, Civil, Electrical, Mechanical]
Difference between add() and offer() method in java
Sl. No. | add() | offer() |
---|---|---|
1 | add() will throw an IllegalStateException if no space is currently available in the Queue, otherwise add method will return true. | offer() method will return false if the element cannot be inserted due to capacity restrictions. |
2 | add() method comes from the Collections framework. | offer() method comes from the queue. |
3 | add() method always returns true. | offer() method always returns false. |
4 | add() method throws an exception when no addition is possible in queue. | offer() method throws true or false if addition is done in the queue. |
Please Login to comment...