[英]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. 我有一个包含一些对象的ArrayList
,例如User
,每个对象都有一个name
和password
属性。 How can I delete only the User
object that has a specific 'name' from this ArrayList
? 如何从此ArrayList
仅删除具有特定“名称”的User
对象?
Iterator<User> it = list.iterator();
while (it.hasNext()) {
User user = it.next();
if (user.getName().equals("John Doe")) {
it.remove();
}
}
Another thought: If User
class can be uniquely defined by the username and if you override equals
with something like: 另一个想法:如果User
类可以由用户名唯一定义,并且如果您用类似以下内容覆盖equals
:
public boolean equals(Object arg0) {
return this.name.equals(((user) arg0).name);
}
You can remove the User
without iterating through the list . 您可以删除User
而无需遍历列表。 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);
You are probably faced with the ConcurrentModificationException
while trying to remove object from the List
. 尝试从List
删除对象时,您可能会遇到ConcurrentModificationException
。 An explanation for this exception is that the iterator of the ArrayList
is a fail-fast iterator. 对此异常的解释是ArrayList
的迭代器是快速失败的迭代器。 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
. 解决此问题的方法是使用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
并删除元素:
List<User> list = new ...
for (Iterator<User> it = list.iterator(); it.hasNext(); ) {
User user = it.next();
if (user.getUserEmail().equals(currentUser.getUserEmail())) {
it.remove();
}
}
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. 使用Iterator删除对象比仅键入ArrayList(Object)
删除效率更高,因为它更快,节省了20%的时间,并且是Java集合的标准Java做法。
You could: 你可以:
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. 只需搜索从用户那里获得的对象的ArrayList,然后测试名称是否等于要删除的名称。 Then remove that object from the list. 然后从列表中删除该对象。
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);
}
}
}
ArrayList<User> userList=new ArrayList<>();
//load data
public void removeUser(String userName){
for (User user: userList){
if(user.getName()equals(userName)){
userList.remove(user);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.