Skip to content

Java ArrayList remove element example

Java ArrayList remove element example shows how to remove an element from ArrayList in Java. The example also shows how to remove all elements or specific elements from ArrayList.

How to remove an element from ArrayList?

To remove an element from the ArrayList, use the remove method.

This method removes an element at specified index from ArrayList. It shifts subsequent elements to the left. The remove method returns the element which was removed from the ArrayList.


You can also use an overloaded remove method which accepts the object to be removed instead of the index.

This method removes the first occurrence of the specified object from the ArrayList. This method returns true if the element was found and removed from the ArrayList, false otherwise.


How to remove objects from ArrayList?

Let’s try to remove objects from the ArrayList.


As you can see from the output, the object is not removed from the ArrayList. The remove method compares the objects to find the object to remove using equals method and Emp class has not implemented equals and hashcode methods. Let’s implement these methods and try again.


How to remove all elements from the ArrayList?

You can use the clear method of the ArrayList class to remove all elements from the ArrayList.


The ArrayList becomes empty after this method call. You can also use removeAll method to remove all elements from the ArrayList as given below.

This example is a part of the Java ArrayList tutorial.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.