Java Deque remove First Occurrence() Method
In this tutorial, we use the removeFirstOccurrence() method within the Deque interface to eliminate the initial instance of a specified element from the deque. If the deque is empty, it remains unaltered. We return a boolean value indicating whether the deque contains the specified element.
Syntax:
boolean removeFirstOccurrence(Object o)
In the above syntax, the o element needs to be removed from the deque, and we return a boolean value TRUE if the element is present in the deque and FALSE if it is not.
Exceptions:
ClassCastException- If the class of the given element is incompatible with the given deque.
NullPointerException- If the specified element is already null and the given deque does not allow the null elements.
Example 1:
In this example, we initialize a Deque using a LinkedList and add several strings to it. We then prints the deque before attempting to remove the first occurrence of the string "Apple". After the removal operation, it checks if the removal was successful and prints a corresponding message. Finally, we print the deque again to display the updated state after the removal operation.
FileName: RemoveFirstOccurence.java
import java.util.Deque; import java.util.LinkedList; public class RemoveFirstOccurence { public static void main(String[] args) { Dequedeque = new LinkedList<>(); deque.add("Apple"); deque.add("Banana"); deque.add("Apple"); deque.add("Orange"); System.out.println("Deque before removal: " + deque); boolean removed = deque.removeFirstOccurrence("Apple"); if (removed) { System.out.println("First occurrence of 'Apple' removed successfully."); } else { System.out.println("No occurrence of 'Apple' found in the Deque."); } System.out.println("Deque after removal: " + deque); } }
Output:
Deque before removal: [Apple, Banana, Apple, Orange] First occurrence of 'Apple' removed successfully. Deque after removal: [Banana, Apple, Orange]
Example 2:
In this example, we initialize a Deque using a LinkedList and add several Integers to it. We then print the deque before attempting to remove the first occurrence of string 20. After the removal operation, it checks if the removal was successful and prints a corresponding message. Finally, we print the deque again to display the updated state after the removal operation.
FileName: RemoveFirstOccurence.java
import java.util.Deque; import java.util.ArrayDeque; public class RemoveFirstOccurence { public static void main(String[] args) { Dequedeque = new ArrayDeque<>(); deque.add(10); deque.add(20); deque.add(30); deque.add(20); deque.add(40); System.out.println("Deque before removal: " + deque); boolean removed = deque.removeFirstOccurrence(20); if (removed) { System.out.println("First occurrence of '20' removed successfully."); } else { System.out.println("No occurrence of '20' found in the Deque."); } System.out.println("Deque after removal: " + deque); } }
Output:
Deque before removal: [10, 20, 30, 20, 40] The first occurrence of '20' was removed successfully. Deque after removal: [10, 30, 20, 40]
Example 3:
In this example, we initialize a Deque using a LinkedList and add several Floats to it. We then print the deque before attempting to remove the first occurrence of the string 20.3f. After the removal operation, it checks if the removal was successful and prints a corresponding message. Finally, we print the deque again to display the updated state after the removal operation.
FileName: RemoveFirstOccurence.java
import java.util.Deque; import java.util.ArrayDeque; public class RemoveFirstOccurence { public static void main(String[] args) { Dequedeque = new ArrayDeque<>(); deque.add(10.5f); deque.add(20.3f); deque.add(30.7f); deque.add(20.3f); deque.add(40.2f); System.out.println("Deque before removal: " + deque); boolean removed = deque.removeFirstOccurrence(20.3f); if (removed) { System.out.println("First occurrence of '20.3' removed successfully."); } else { System.out.println("No occurrence of '20.3' found in the Deque."); } System.out.println("Deque after removal: " + deque); } }
Output:
Deque before removal: [10.5, 20.3, 30.7, 20.3, 40.2] First occurrence of '20.3' removed successfully. Deque after removal: [10.5, 30.7, 20.3, 40.2]