Open In App

Queue offer() method in Java

Last Updated : 26 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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.




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


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.




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


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.




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


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.




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


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.




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


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-



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads