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
?

- 12,959
- 7
- 83
- 96

- 145
- 1
- 2
- 12
-
3Can you be more accurate describing your question ? write a snippet of the code you are working on ... – aleroot May 08 '12 at 16:02
9 Answers
Iterator<User> it = list.iterator();
while (it.hasNext()) {
User user = it.next();
if (user.getName().equals("John Doe")) {
it.remove();
}
}

- 486,780
- 108
- 951
- 1,012
-
9Doesn't look like the OP did any work for this question, you shouldn't give them code for free. – Hunter McMillen May 08 '12 at 16:04
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"))
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);

- 3,521
- 1
- 17
- 27
-
-
s is predicate here and this only works in JDK 1.8 where Lambda expression is supported.Removes all of the elements of this collection that satisfy the given predicate. – jprism Apr 11 '17 at 17:32
-
-
Thanks . Your answer helped me a lot to graduate from computer science. I gave you a badge for the best answer after 8 years xD – Mohamed Gamal Jun 17 '20 at 10:51
-
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();
}
}

- 12,959
- 7
- 83
- 96

- 2,426
- 26
- 22
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.

- 3,322
- 25
- 30
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.

- 321,522
- 82
- 660
- 783
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.

- 59,865
- 24
- 119
- 170
-
One hopes there are optimizations that can be made, as otherwise the OP is stuck with an O(n) implementation. – Clockwork-Muse May 08 '12 at 16:04
-
1@X-Zero In what scenario where you dont know where an object is in a list could you have better than `O(n)`? – Hunter McMillen May 08 '12 at 16:05
-
2@X-Zero: If all the OP has is an array list, then `O(n)` is the best he can hope for (element removal alone is `O(n)`). – NPE May 08 '12 at 16:05
-
... Yes, I know. But it'd be nice if we knew it was sorted (by name), or maybe if it was a hashmap or somesuch. – Clockwork-Muse May 08 '12 at 16:09
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);
}
}
}

- 12,959
- 7
- 83
- 96

- 27
- 1
- 5
-
2You should use an iterator to remove within a loop or you will likely get a ConcurrentModificationException. – assylias May 08 '12 at 17:06
-
@assylias The basic for loop was extended in Java 5 to make iteration over arrays and other collections more convenient. And the for-each loop implicity use the iterator – Matrix Scarlett Johansson May 08 '12 at 17:22
-
1Try to run this code (sorry for the formatting): `List
list = new ArrayList – assylias May 08 '12 at 17:26(); list.add("a"); for (String s : list) { list.remove(s); }` and you should get an exception. -
@assylias aha that's right I just thought for-each use the iterator but I ignored the fact that list.remove not the iterator.remove unless for-each can explore the iterator object to do the remove function – Matrix Scarlett Johansson May 08 '12 at 17:45
ArrayList<User> userList=new ArrayList<>();
//load data
public void removeUser(String userName){
for (User user: userList){
if(user.getName()equals(userName)){
userList.remove(user);
}
}
}

- 4,406
- 9
- 45
- 77

- 76
- 5
-
2Can't remove while iterating through for each http://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re – Gangaraju Dec 29 '16 at 06:06