Mohamed Gamal
Mohamed Gamal

Reputation: 145

How do I remove an object from an ArrayList in Java?

I have an ArrayList that contains some object, such as User, and each object has a name and password property. How can I delete only the User object that has a specific 'name' from this ArrayList?

Upvotes: 7

Views: 56572

Answers (9)

Manikant Gautam
Manikant Gautam

Reputation: 3591

You could use something like this:

           // If you are using java 8
           userList.removeIf(user-> user.getName().equals("yourUserName"));
           // With older version
           User userToRemove = null;
           for(User usr:userList) {
             if(usr.getName().equals("yourUserName")) {
                userToRemove = usr;
                break;
             }
           }
           userList.remove(userToRemove);

Upvotes: 7

hari_sree
hari_sree

Reputation: 1538

Another thought: If User class can be uniquely defined by the username and if you override equals with something like:

public boolean equals(Object arg0) {
    return this.name.equals(((user) arg0).name);
}

You can remove the User without iterating through the list . You can just do :

 list.remove(new User("John Doe"))

Upvotes: 7

Anjali Pavithra
Anjali Pavithra

Reputation: 72

ArrayList<User> userList=new ArrayList<>();
//load data

public void removeUser(String userName){
    for (User user: userList){
        if(user.getName()equals(userName)){
            userList.remove(user);
        }
    }
}

Upvotes: -1

Rahul Raina
Rahul Raina

Reputation: 3460

Recommended way to solve this problem is:

ArrayList<User> list = new ArrayList<User>();
list.add(new User("user1","password1"));
list.add(new User("user2","password2"));
list.add(new User("user3","password3"));
list.add(new User("user4","password4"));
Iterator<String> iter = list.iterator();
while (iter.hasNext()) 
{
    User user = iter.next();
    if(user.name.equals("user1"))
    {
        //Use iterator to remove this User object.
        iter.remove();
    }
}

Using Iterator to remove an object is more efficient than removing by simply typing ArrayList(Object) because it is more faster and 20% more time saving and a standard Java practice for Java Collections.

Upvotes: 6

miksiii
miksiii

Reputation: 2496

You are probably faced with the ConcurrentModificationException while trying to remove object from the List. An explanation for this exception is that the iterator of the ArrayList is a fail-fast iterator. For example, it will throw an exception (fail) when it detects that its collection in the runtime has been modified. The solution to this problem is to use the Iterator.

Here is a simple example that demonstrate how you could iterate through the List and remove the element when specific condition is met:

List<User> list = new ...

for (Iterator<User> it = list.iterator(); it.hasNext(); ) {

    User user = it.next();
    if (user.getUserEmail().equals(currentUser.getUserEmail())) {
       it.remove();
    }
}

Upvotes: 6

Your code might look like this:

List<User> users = new ArrayList<User>();

public void removeUser(String name){
    for(User user:users){
        if(user.name.equals(name)){
            users.remove(user);
        }
    }
}

Upvotes: 0

NPE
NPE

Reputation: 500903

Iterator<User> it = list.iterator();
while (it.hasNext()) {
  User user = it.next();
  if (user.getName().equals("John Doe")) {
    it.remove();
  }
}

Upvotes: 24

assylias
assylias

Reputation: 328893

You could:

  • loop over the list with an iterator
  • check if each item in your list is the right user (checking the name)
  • if it is, use the remove method of the iterator.

Upvotes: 3

Hunter McMillen
Hunter McMillen

Reputation: 61540

Just search through the ArrayList of objects you get from the user, and test for a name equal to the name you want to remove. Then remove that object from the list.

Upvotes: 1

Related Questions