Home >Java >javaTutorial >In Java, what is the difference between add() method and offer() method in queue?
Queue in Java is a linear data structure with multiple functions. A queue has two endpoints and it follows the first-in-first-out (FIFO) principle for inserting and deleting its elements. In this tutorial, we will learn about two important functions of queue in Java, they are add() and Offer().
The queue in java is an interface that extends the util and collection packages. Elements are inserted in the backend and removed from the frontend. Queues in Java can be implemented using classes such as linked lists, DeQueue, and priority queues. A priority queue is an extended form of a normal queue, where each element has a priority.
This method is used to insert elements into the queue. It adds the defined element (the element passed as argument) to the end of the queue and returns true only if the defined element is successfully added to the end. The add() method throws an exception if the element is not added to the end of the queue.
Using this method, we can add integer and string values to the queue.
Example: add(3) This will insert 3 at the end of the queue.
The add() method always takes some parameter values. You cannot pass null values to it because Queue does not accept Null values, in which case it will throw an exception.
IllegalStateException - This java exception occurs when the queue reaches its maximum capacity.
NullPointerException - When trying to enter a null value through the add() method because the queue does not accept null values.
The following program shows how to implement the add() method in Queue in Java.
import java.util.*; // importing util package with all its features public class Main { public static void main(String[] args) { Queue<Integer> q = new LinkedList<>(); // queue declaration q.add(5); //adding elements to the queue q.add(6); q.add(4); q.add(1); q.add(8); System.out.println("Queue is: " + q); } }
Queue is: [5, 6, 4, 1, 8]
This method is used to insert elements into the queue. The elements can be integer or string data types. It inserts the specified element based on the queue's capacity. It does not throw any exception if a specific element cannot be inserted into the queue.
It returns True when an element is successfully inserted into the queue backend in Java. If the queue exceeds its capacity, the offer() method returns false.
offer(3) : this will insert 3 into the queue offer(“Java”) : this will insert Java into the queue
The following program shows how to implement offer() in java.
import java.util.*; // importing util package with all its features public class Main { public static void main(String[] args) { Queue<String> q = new LinkedList<>(); // queue declaration q.offer("Java"); //inserting elements to the queue q.offer("is"); q.offer("Good"); System.out.println("Queue is " + q); } }
Queue is [Java, is, Good]
S.No |
add() function |
offer() method |
---|---|---|
1 |
The add() function throws an IllegalState exception when you try to insert an element into a queue that is full. |
When the queue is full or reaches the maximum size, it does not throw any exception but returns false. |
2 |
After successfully inserting the queue element, the add() method returns true. It will not return False |
offer() method returns True when the element is successfully inserted and False when the insertion of the Queue element fails. |
3 |
Belongs to the Collection framework. |
This is a queue method. |
The only difference between the add() and Offer() methods in Queue is that if add() exceeds the limit of the queue, an exception will be thrown. Although the Offer() method does not throw any exception, it returns true if the element is successfully inserted and False if the element cannot be inserted into the queue because the queue has reached maximum capacity.
The above is the detailed content of In Java, what is the difference between add() method and offer() method in queue?. For more information, please follow other related articles on the PHP Chinese website!