Deque ofrece el último método() en Java

El método offerLast(E e) de Deque Interface inserta el elemento especificado al final de 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 offerLast(E e)

Parámetros: Este método acepta un parámetro obligatorio e que es el elemento a insertar al final del Deque.

Devoluciones: este método devuelve verdadero en la inserción exitosa; de lo contrario, devuelve falso.

Excepciones: la función arroja tres 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 offerLast() de Deque:

Programa 1: Con la ayuda de LinkedList .

// Java Program Demonstrate offerLast()
// 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.offerLast(10))
            System.out.println("The Deque is not full and 10 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offerLast(15))
            System.out.println("The Deque is not full and 15 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offerLast(25))
            System.out.println("The Deque is not full and 25 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offerLast(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);
    }
}
Producción:

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 offerLast()
// 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.offerLast(10))
            System.out.println("The Deque is not full and 10 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offerLast(15))
            System.out.println("The Deque is not full and 15 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offerLast(25))
            System.out.println("The Deque is not full and 25 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offerLast(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);
    }
}
Producción:

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 offerLast()
// 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.offerLast(10))
            System.out.println("The Deque is not full and 10 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offerLast(15))
            System.out.println("The Deque is not full and 15 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offerLast(25))
            System.out.println("The Deque is not full and 25 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offerLast(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);
    }
}
Producción:

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 offerLast()
// 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.offerLast(10))
            System.out.println("The Deque is not full and 10 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offerLast(15))
            System.out.println("The Deque is not full and 15 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offerLast(25))
            System.out.println("The Deque is not full and 25 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offerLast(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);
    }
}
Producción:

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 offerLast()
// 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.offerLast(7855642);
        DQ.offerLast(35658786);
        DQ.offerLast(5278367);
  
        // when null is inserted
        DQ.offerLast(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 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#offerLast-E-

Publicación traducida automáticamente

Artículo escrito por gopaldave y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *