remove() method

remove() method: This method is used to remove the given element in collection.

boolean remove(Object obj);

Where, Object in java represents the type of class in the collection.
obj is the element that you want to remove from the collection.
return type:  Its return type is boolean. It can return either true or false.

How remove() method can work: It removes a single instance of the specified element from this collection. There may be any collection that can contains one or more such elements, but it removes only one which found first. It returns true if item is removed successfully but if item doesn’t exist it will returns the false.

This method can throw different types of exceptions:
ClassCastException, If the type of the given element is incompatible for the collection.
NullPointerException, If the specified element is null and this collection does not permit null elements.
UnsupportedOperationException, If remove operation is not allowed by this collection.

Leave a Comment

Follow us on Instagram & watch the latest videos on YouTube. Click below social icons to visit our Instagram & YouTube profiles.