Is there any difference between the remove(Object o) method (of List interface) and removeFirstOccurrence(Object o) method (of LinkedList class) in the collections api?
I could see that both does the same i.e remove first occurrence of the object in the list.
Is there any difference between remove(Object o) method (of List interface) and removeFirstOccurrence(Object o) method (of LinkedList class) in collections framework?
These are two distinct methods, coming from two distinct interfaces.
The first one (remove(Object o)) is defined in the java.util.Collection interface.
The other one (removeFirstOccurrence(Object o) is defined in the java.util.Deque interface.
The first one (remove(Object o)) has a contract rather general in the Collection interface :
Removes a single instance of the specified element from this collection, if it is present...
But the List interface that extends Collection has a more specific contract :
Removes the first occurrence of the specified element from this list, if it is present (optional operation)....
One the other hand, the removeFirstOccurrence(Object o) defined in the Deque interface specifies a similar contract :
Removes the first occurrence of the specified element from this deque...
It turns out that the LinkedList implements both directly List and Deque.
And as List.remove(Object o) and Deque.removeFirstOccurrence(Object o) specify a similar contract, it is really not surprising that the behavior and the implementation of these two methods in the LinkedList class be the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With