Java List Remove Object Example

In this example we will discuss how to remove objects from a java.util.List.

There are 2 methods described in the Java API to do so :

  • E remove(int index)

We can use index of list to remove the object.

  • boolean remove(Object o)


We can pass the object itself which needs to be removed. For this method to work for objects we have to make sure that the equals() method is properly overridden.

Lets see both of them in examples.

1) remove(int index)

		// making a list of string objects
		List strList = new ArrayList();

		for (String string : strList) {


		System.out.println("**** After Removal by Index ****");

		for (String string : strList) {

		// making a list of user objects
		List userList = new ArrayList();
		userList.add(new User("abcd", "pqrs"));
		userList.add(new User("abcd1", "pqrs1"));
		userList.add(new User("abcd2", "pqrs2"));

		for (User user : userList) {

		//removing user by index
		System.out.println("**** After Removal by Index for User List ****");
		for (User user : userList) {

2) remove(Object o)

		// remove String from string list

		System.out.println("**** After Removal by Object ****");

		for (String string : strList) {

                //removing by user object
		userList.remove(new User("abcd1","pqrs1"));
		System.out.println("**** After Removal by User Object ****");
		for (User user : userList) {

The User Class

package com.javacodegeeks.example;

public class User {

	private String firstName;
	private String lastName;

	public User(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
        //setters and getters
	public String toString() {
		return firstName + " " + lastName;

	public boolean equals(Object obj) {
		return (this.firstName.equals(((User) obj).firstName) && (this.lastName
				.equals(((User) obj).lastName)));

In the above example, we passed the object which was to be removed from the ArrayList. The important point to note here is that we had to make sure that the equals method was overridden properly for the remove(object) method to work.

Download the Eclipse project of this tutorial

You can download the full source code of this example here :

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Anirudh Bhatnagar

Anirudh is a Java programmer with extensive experience in building Java/J2EE applications. He has always been fascinated by the new technologies and emerging trends in software development. He has been involved in propagating these changes and new technologies in his projects. He is an avid blogger and agile enthusiast who believes in writing clean and well tested code.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button