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?
-
3Can you be more accurate describing your question ? write a snippet of the code you are working on ...aleroot– aleroot2012-05-08 16:02:12 +00:00Commented May 8, 2012 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();
}
}
1 Comment
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);
5 Comments
s? Please make your answers a bit more specific.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"))
Comments
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();
}
}
Comments
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.
Comments
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.
4 Comments
O(n)?O(n) is the best he can hope for (element removal alone is O(n)).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);
}
}
}
4 Comments
List<String> list = new ArrayList<String>(); list.add("a"); for (String s : list) { list.remove(s); } and you should get an exception.ArrayList<User> userList=new ArrayList<>();
//load data
public void removeUser(String userName){
for (User user: userList){
if(user.getName()equals(userName)){
userList.remove(user);
}
}
}