Queue offer() method in Java

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:

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.

Below programs illustrate offer() method of Queue:

Program 1: With the help of LinkedBlockingDeque.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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]

Program 2: With the help of ConcurrentLinkedDeque.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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]

Program 3: With the help of ArrayDeque.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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]

Program 4: With the help of LinkedList.


filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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]

Below programs illustrate exceptions thrown by this method:

Program 5: To show NullPointerException.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Exception: java.lang.NullPointerException

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-



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.