El método removeFirstOccurrence() de LinkedBlockingDeque elimina la primera ocurrencia del elemento especificado de este deque. Si el deque no contiene el elemento, permanece sin cambios. Devuelve verdadero si este deque contenía el elemento especificado, de lo contrario, devuelve falso.
Sintaxis:
public boolean removeFirstOccurrence(Object o)
Parámetros: este método acepta un parámetro obligatorio que especifica el elemento que se eliminará del contenedor Deque.
Devoluciones: este método devuelve verdadero si el elemento está presente y se eliminó del contenedor Deque; de lo contrario, devuelve falso .
Los siguientes programas ilustran el método removeFirstOccurrence() de LinkedBlockingDeque:
Programa 1: cuando el elemento está presente
Java
// Java Program to demonstrate removeFirstOccurrence() // method of LinkedBlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of LinkedBlockingDeque LinkedBlockingDeque<Integer> LBD = new LinkedBlockingDeque<Integer>(); // Add numbers to end of LinkedBlockingDeque LBD.add(15); LBD.add(20); LBD.add(20); LBD.add(15); // print Dequeue System.out.println("Linked Blocking Deque: " + LBD); if (LBD.removeFirstOccurrence(15)) System.out.println("First occurrence of 15 removed"); else System.out.println("15 not present and not removed"); // prints the Deque after removal System.out.println("Linked Blocking Deque: " + LBD); } }
Linked Blocking Deque: [15, 20, 20, 15] First occurrence of 15 removed Linked Blocking Deque: [20, 20, 15]
Programa 2: Cuando el elemento no está presente
Java
// Java Program to demonstrate removeFirstOccurrence() // method of LinkedBlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of LinkedBlockingDeque LinkedBlockingDeque<Integer> LBD = new LinkedBlockingDeque<Integer>(); // Add numbers to end of LinkedBlockingDeque LBD.add(15); LBD.add(20); LBD.add(20); LBD.add(15); // print Dequeue System.out.println("Linked Blocking Deque: " + LBD); if (LBD.removeFirstOccurrence(10)) System.out.println("First occurrence of 10 removed"); else System.out.println("10 not present and not removed"); // prints the Deque after removal System.out.println("Linked Blocking Deque: " + LBD); } }
Linked Blocking Deque: [15, 20, 20, 15] 10 not present and not removed Linked Blocking Deque: [15, 20, 20, 15]