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

后端 未结 9 1642
面向向阳花
面向向阳花 2020-12-15 01:06

I have an ArrayList that contains some object, such as User, and each object has a name and password property. How can I

相关标签:
9条回答
  • 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.

    0 讨论(0)
  • 2020-12-15 01:11
    Iterator<User> it = list.iterator();
    while (it.hasNext()) {
      User user = it.next();
      if (user.getName().equals("John Doe")) {
        it.remove();
      }
    }
    
    0 讨论(0)
  • 2020-12-15 01:22
    ArrayList<User> userList=new ArrayList<>();
    //load data
    
    public void removeUser(String userName){
        for (User user: userList){
            if(user.getName()equals(userName)){
                userList.remove(user);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 01:25

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-15 01:27

    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.
    0 讨论(0)
  • 2020-12-15 01:31

    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);
    
    0 讨论(0)
提交回复
热议问题