boolean remove(Object o) This method removes the specified element from the deque.
remove(Object o) method of Deque Interface in Java.
In the following example we will show how can remove(Object o) method be implemented in Deque Interface in java.
Syntax
boolean remove(Object o)
This method deletes a specific element from the deque.
In this method the first presence of a specific element is to be removed from the underlying deque. The deque remains unchanged if the specified object does not exists in it. Generally at first it matches the specified element into the deque and then, if the element matches, it removes the first occurrence of that element and returns 'true' otherwise, it does not put any effect onto the deque and return 'false'. This method is equivalent to the removeFirstOccurrence(Object o).
Parameter description
o : It takes an element what do you want to remove from deque.
Example of remove(Object o) method
In this example we will show you how does remove(Object o) method work in Deque interface. Through this example we will show how you can delete the first occurrence of the specified element from the underlying deque, and count the total number of elements in deque before and after removing the elements.
Example :
package devmanuals.com; import java.util.Deque; import java.util.LinkedList; public class DequeRemoveObject { public static void main(String[] args) { Deque dq = new LinkedList(); dq.add(1); dq.add("Dev"); dq.add("Manuals"); dq.add("Manuals"); dq.add(".com"); System.out.println("Elements of deque : " + dq); System.out.println("Size of deque before removing element : "+dq.size()); // Implementation of the method boolean bol = dq.remove("Manuals"); if (bol == true) { System.out.println("The element is matched and "); System.out.println("removed it from deque then new deque = " + dq); System.out.println("Element is Removed = " + bol); System.out.println("Size of deque = " + dq.size()); } else { System.out.println("The element did not matched "); System.out.println("therefore deque remains unchanged " + dq); System.out.println("Element is Removed = " + bol); System.out.println("Size of deque = " + dq.size()); } } }
Output :
Elements of deque : [1, Dev, Manuals, Manuals, .com] Size of deque before removing element : 5 The element is matched and removed it from deque then new deque = [1, Dev, Manuals, .com] Element is Removed = true Size of deque = 4 |
[ 0 ] Comments