El método de oferta (E e) de la interfaz Deque inserta el elemento especificado en este Deque 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 el Deque.
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 al Deque.
- NullPointerException : cuando el elemento a insertar se pasa como nulo y la interfaz de Deque no permite elementos nulos.
Los siguientes programas ilustran el método de oferta() de Deque:
Programa 1: Con la ayuda de LinkedList .
// Java Program Demonstrate offer() // method of Deque when Null is passed import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Deque Deque<Integer> DQ = new LinkedList<Integer>(); if (DQ.offer(10)) System.out.println("The Deque is not full and 10 is inserted"); else System.out.println("The Deque is full"); if (DQ.offer(15)) System.out.println("The Deque is not full and 15 is inserted"); else System.out.println("The Deque is full"); if (DQ.offer(25)) System.out.println("The Deque is not full and 25 is inserted"); else System.out.println("The Deque is full"); if (DQ.offer(20)) System.out.println("The Deque is not full and 20 is inserted"); else System.out.println("The Deque is full"); // before removing print Deque System.out.println("Deque: " + DQ); } }
The Deque is not full and 10 is inserted The Deque is not full and 15 is inserted The Deque is not full and 25 is inserted The Deque is not full and 20 is inserted Deque: [10, 15, 25, 20]
Programa 2: Con la ayuda de ArrayDeque .
// Java Program Demonstrate offer() // method of Deque when Null is passed import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Deque Deque<Integer> DQ = new ArrayDeque<Integer>(); if (DQ.offer(10)) System.out.println("The Deque is not full and 10 is inserted"); else System.out.println("The Deque is full"); if (DQ.offer(15)) System.out.println("The Deque is not full and 15 is inserted"); else System.out.println("The Deque is full"); if (DQ.offer(25)) System.out.println("The Deque is not full and 25 is inserted"); else System.out.println("The Deque is full"); if (DQ.offer(20)) System.out.println("The Deque is not full and 20 is inserted"); else System.out.println("The Deque is full"); // before removing print Deque System.out.println("Deque: " + DQ); } }
The Deque is not full and 10 is inserted The Deque is not full and 15 is inserted The Deque is not full and 25 is inserted The Deque is not full and 20 is inserted Deque: [10, 15, 25, 20]
Programa 3: Con la ayuda de ConcurrentLinkedDeque .
// Java Program Demonstrate offer() // method of Deque when Null is passed import java.util.*; import java.util.concurrent.ConcurrentLinkedDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Deque Deque<Integer> DQ = new ConcurrentLinkedDeque<Integer>(); if (DQ.offer(10)) System.out.println("The Deque is not full and 10 is inserted"); else System.out.println("The Deque is full"); if (DQ.offer(15)) System.out.println("The Deque is not full and 15 is inserted"); else System.out.println("The Deque is full"); if (DQ.offer(25)) System.out.println("The Deque is not full and 25 is inserted"); else System.out.println("The Deque is full"); if (DQ.offer(20)) System.out.println("The Deque is not full and 20 is inserted"); else System.out.println("The Deque is full"); // before removing print Deque System.out.println("Deque: " + DQ); } }
The Deque is not full and 10 is inserted The Deque is not full and 15 is inserted The Deque is not full and 25 is inserted The Deque is not full and 20 is inserted Deque: [10, 15, 25, 20]
Programa 4: Con la ayuda de LinkedBlockingDeque .
// Java Program Demonstrate offer() // method of Deque when Null is passed import java.util.*; import java.util.concurrent.LinkedBlockingDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Deque Deque<Integer> DQ = new LinkedBlockingDeque<Integer>(); if (DQ.offer(10)) System.out.println("The Deque is not full and 10 is inserted"); else System.out.println("The Deque is full"); if (DQ.offer(15)) System.out.println("The Deque is not full and 15 is inserted"); else System.out.println("The Deque is full"); if (DQ.offer(25)) System.out.println("The Deque is not full and 25 is inserted"); else System.out.println("The Deque is full"); if (DQ.offer(20)) System.out.println("The Deque is not full and 20 is inserted"); else System.out.println("The Deque is full"); // before removing print Deque System.out.println("Deque: " + DQ); } }
The Deque is not full and 10 is inserted The Deque is not full and 15 is inserted The Deque is not full and 25 is inserted The Deque is not full and 20 is inserted Deque: [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.LinkedBlockingDeque; public class GFG { public static void main(String[] args) throws NullPointerException { // create object of Queue Deque<Integer> DQ = new LinkedBlockingDeque<Integer>(); // Add numbers to end of Deque DQ.offer(7855642); DQ.offer(35658786); DQ.offer(5278367); // when null is inserted DQ.offer(null); // before removing print Deque System.out.println("Deque: " + DQ); } }
Producción:
Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.LinkedBlockingDeque.offerLast(LinkedBlockingDeque.java:357) at java.util.concurrent.LinkedBlockingDeque.offer(LinkedBlockingDeque.java:641) at GFG.main(GFG.java:21)
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/Deque.html#offer-E-