Home » Core Java » util » List » Java List Remove Object Example

About 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.

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();
		strList.add("one");
		strList.add("two");
		strList.add("three");

		for (String string : strList) {
			System.out.println(string);
		}

		strList.remove(1);

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

		for (String string : strList) {
			System.out.println(string);
		}

		// 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) {
			System.out.println(user);
		}

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

2) remove(Object o)

		// remove String from string list
		strList.remove("one");

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

		for (String string : strList) {
			System.out.println(string);
		}

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

The User Class

package com.javacodegeeks.example;

public class User {

	private String firstName;
	private String lastName;

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

	@Override
	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

Download
You can download the full source code of this example here : JavaListRemoveObjectExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments