Java Collection remove() Method

The remove() method of Java Collection Interface is used to remove a single instance of the specified element from this collection.

Syntax

Parameters

The parameter 'o' represents the element to be removed from this collection if it is present.

Throws

ClassCastException- if the type of the specified element is not compatible with this collection

NullPointerException- if the specified element is null and this collection does not allow null elements

Returns

The remove() method returns a boolean value 'true' if the specified element is successfully removed as a result of this call.

Example 1

Test it Now

Output:

52
532
5
15
After removing 52 
Collection : [532, 5, 15]

Example 2

Test it Now

Output:

Reema
Geetanajli
Mahesh
Ajeet
remove method will return : false

Example 3

Test it Now

Output:

Exception in thread "main" java.lang.NullPointerException
	at java.util.ArrayDeque.addLast(ArrayDeque.java:249)
	at java.util.ArrayDeque.add(ArrayDeque.java:423)
	at com.javaTpoint.JavaCollectionRemoveExample3.main(JavaCollectionRemoveExample3.java:13)
Next TopicJava-Collection




Latest Courses