A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/queue-offer-method-in-java/ below:

Queue offer() method in Java

Queue offer() method in Java

Last Updated : 11 Jul, 2025

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:

Below programs illustrate offer() method of Queue:

Program 1:

With the help of

LinkedBlockingDeque

.

Java
// 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
// 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
// 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
// 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
// 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-

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4