El método removeFirstOccurrence() de BlockingDeque 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 .
Nota : El método removeFirstOccurrence() de BlockingDeque se ha heredado de la clase LinkedBlockingDeque en Java.
Los siguientes programas ilustran el método removeFirstOccurrence() de BlockingDeque:
Programa 1: cuando el elemento está presente
Java
// Java Program to demonstrate removeFirstOccurrence() // method of BlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.BlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of BlockingDeque BlockingDeque<Integer> BD = new LinkedBlockingDeque<Integer>(); // Add numbers to end of BlockingDeque BD.add(15); BD.add(20); BD.add(20); BD.add(15); // print Deque System.out.println("Blocking Deque: " + BD); if (BD.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("Blocking Deque: " + BD); } }
Blocking Deque: [15, 20, 20, 15] First occurrence of 15 removed Blocking Deque: [20, 20, 15]
Programa 2: Cuando el elemento no está presente
Java
// Java Program to demonstrate removeFirstOccurrence() // method of BlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.BlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of BlockingDeque BlockingDeque<Integer> BD = new LinkedBlockingDeque<Integer>(); // Add numbers to end of BlockingDeque BD.add(15); BD.add(20); BD.add(20); BD.add(15); // print Deque System.out.println("Blocking Deque: " + BD); if (BD.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("Blocking Deque: " + BD); } }
Blocking Deque: [15, 20, 20, 15] 10 not present and not removed Blocking Deque: [15, 20, 20, 15]