Difference Between add() & offer() methods in Queue in Java?


Queue in Java is a linear data structure with various functions. A Queue has two endpoints and it follows the First-In-First-Out (FIFO)principle to insert and remove its elements. In this tutorial, we will look at two important functions of the queue in Java and they are add() and offer().

What is Queue?

A queue in java is an interface that extends the util and collection packages. The elements are inserted at the Rear end and removed from the Front end. A queue in java can be implemented by using the classes of linked list, DeQueue, and priority queue. A priority queue is an extended form of a normal queue with a priority for each element.

add() Method of Queue

This method is used to insert elements into the queue. It adds the defined element (element passed as a parameter) at the end of the queue and it returns true only when the defined element is successfully added at the end. If an element is not added to the queue’s end, the add() method throws an exception.

Using this method, we can add integer and string values to the queue.

For Example: add(3) this will insert 3, at the end of the queue.

add() method always takes some parameter value. You cannot pass a null value to it as Queue does not accept Null values and in this case, it will throw an exception.

Types of Exception in add() method

  • IllegalStateException − This java exception arises when the queue reaches its maximum capacity.

  • NullPointerException − When one tries to enter null value through add() method as the queue does not accept null value.

Example

The following program show how to implement 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);
   }
}

Output

Queue is: [5, 6, 4, 1, 8]

offer() Method in Queue

This method is used to insert elements into the queue, the elements can be integer or string data type. It inserts the specified element as per the capacity of the queue. It does not throw any exception if particular elements cannot be inserted into the queue.

It returns True on successful insertion of elements on the Rear end of the queue in Java. If the queue is out of its capacity, the offer() method will return false.

For example

offer(3) : this will insert 3 into the queue
offer(“Java”) : this will insert Java into the queue

Example

The following program show 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);
   
	}
}

Output

Queue is [Java, is, Good]

Difference Between add() and offer() Method

S.No

add() function

offer() method

1

add() function throws an IllegalState exception when you try to insert an element in a full queue.

It does not throw any exception but returns false when the queue is full or reaches its maximum size.

2

On successful inserting a queue element, add() method returns true. It does not return False

offer() method returns True on successfully inserting an element and returns False when it fails to insert a Queue element.

3

It belongs to the Collection framework.

It is a queue method.

Conclusion

The only difference between add() and offer() method in Queue is that: add() throws an exception if it extends the limit of the queue. While the offer() method does not throw any exception, it returns true on successful insertion of the element and returns False when element fails to insert into the queue due to the queue reaching its highest capacity.

Updated on: 22-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements