El método de oferta (E e) de la interfaz de cola inserta el elemento especificado en esta cola si es posible hacerlo inmediatamente sin violar las restricciones de capacidad. Este método es preferible al método add() ya que este método no arroja una excepción cuando la capacidad del contenedor está llena ya que devuelve falso.
Sintaxis:
boolean offer(E e)
Parámetros: Este método acepta un parámetro obligatorio e que es el elemento a insertar en la Cola.
Devoluciones: este método devuelve verdadero en la inserción exitosa; de lo contrario, devuelve falso.
Excepciones: la función arroja cuatro excepciones que se describen a continuación:
- ClassCastException : cuando la clase del elemento a ingresar impide que se agregue a este contenedor.
- IllegalArgumentException : cuando alguna propiedad del elemento impide que se agregue a la cola.
- NullPointerException : cuando el elemento a insertar se pasa como nulo y la interfaz de Queue no permite elementos nulos.
Los siguientes programas ilustran el método de oferta() de la cola:
Programa 1: Con la ayuda de 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); } }
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]
Programa 2: Con la ayuda de 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); } }
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]
Programa 3: Con la ayuda de 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); } }
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]
Programa 4: Con la ayuda de 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); } }
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]
Los siguientes programas ilustran las excepciones lanzadas por este método :
Programa 5: Para mostrar 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); } } }
Exception: java.lang.NullPointerException
Nota: Las otras dos excepciones son internas y dependen del compilador, por lo que no se pueden mostrar en el código.
Referencia: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#offer-E-