El método offerFirst(E e) de LinkedBlockingDeque inserta el elemento pasado en el parámetro al frente del contenedor Deque. Si la capacidad del contenedor ha excedido, entonces no devuelve una excepción como en el caso de la función add() y addFirst().
Sintaxis:
public boolean offerFirst(E e)
Parámetros: este método acepta un parámetro obligatorio e que es el elemento que se insertará al principio de LinkedBlockingDeque.
Devoluciones: este método devuelve verdadero si el elemento se ha insertado, de lo contrario, devuelve falso.
Los siguientes programas ilustran el método offerFirst() de LinkedBlockingDeque:
Programa 1:
Java
// Java Program Demonstrate offerFirst() // method of LinkedBlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of LinkedBlockingDeque LinkedBlockingDeque<Integer> LBD = new LinkedBlockingDeque<Integer>(4); // Add numbers to end of LinkedBlockingDeque LBD.offerFirst(7855642); LBD.offerFirst(35658786); LBD.offerFirst(5278367); LBD.offerFirst(74381793); // Cannot be inserted LBD.offerFirst(10); // cannot be inserted hence returns false if (!LBD.offerFirst(10)) System.out.println("The element 10 cannot be inserted"+ " as capacity is full"); // before removing print queue System.out.println("Linked Blocking Deque: " + LBD); } }
Producción:
The element 10 cannot be inserted as capacity is full Linked Blocking Deque: [74381793, 5278367, 35658786, 7855642]
Programa 2:
Java
// Java Program Demonstrate offerFirst() // method of LinkedBlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of LinkedBlockingDeque LinkedBlockingDeque<String> LBD = new LinkedBlockingDeque<String>(4); // Add numbers to end of LinkedBlockingDeque LBD.offerFirst("abc"); LBD.offerFirst("gopu"); LBD.offerFirst("geeks"); LBD.offerFirst("richik"); // Cannot be inserted LBD.offerFirst("hii"); // cannot be inserted hence returns false if (!LBD.offerFirst("hii")) System.out.println("The element 'hii' cannot be" +" inserted as capacity is full"); // before removing print queue System.out.println("Linked Blocking Deque: " + LBD); } }
Producción:
The element 'hii' cannot be inserted as capacity is full Linked Blocking Deque: [richik, geeks, gopu, abc]
Referencia: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#offerFirst(E)