更新数组列表java中的特定对象项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32993540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:08:09  来源:igfitidea点击:

Update specific object items inside array list java

javaarrays

提问by Cris

I would like to ask a question how can i update the record from my array list object:

我想问一个问题,如何从我的数组列表对象更新记录:

e.g:

例如:

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

User user = new User();
user.setUserId(1);
user.setUsername("user1");
userList.add(user);

User user = new User();
user.setUserId(2);
user.setUsername("user2");
userList.add(user);

User user = new User();
user.setUserId(3);
user.setUsername("user3");
userList.add(user);

Now i want to update the specific records on my array list, let say I want to update the username of user id #2 e.g:

现在我想更新我的数组列表中的特定记录,假设我想更新用户 ID #2 的用户名,例如:

User user = new User();
user.setUserId(2);
user.setUsername("new_username2");

//before i want to remove or update the record on the list which contain user id #2
userList.add(user);

something like i want to search from the list that userList.contains(2) then remove or update it with the new values.

像我想从 userList.contains(2) 列表中搜索然后删除或使用新值更新它的东西。

Thanks in advance.

提前致谢。

采纳答案by javatutorial

In your case I think it's better using a Map instead of a List:

在您的情况下,我认为最好使用 Map 而不是 List:

Map<Integer, User> userMap = new HashMap<Integer, User>();

User user = new User();
user.setUserId(1);
user.setUsername("user1");
userMap.put(user.getUserId(), user);

user = new User();
user.setUserId(2);
user.setUsername("user2");
userMap.put(user.getUserId(), user);

user = new User();
user.setUserId(3);
user.setUsername("user3");
userMap.put(user.getUserId(), user);

In this way, you can search directly for the userId you need:

这样就可以直接搜索你需要的userId:

User userToModify = userMap.remove(idToModify);
userToModify.setUsername("new name");
userToModify.setUserId(54);
userMap.put(user.getUserId(), userToModify);

If you need to find object only by one field (userId, in this case), a Map is far more efficient and easy to use (and to maintain).

如果您只需要通过一个字段(在本例中为 userId)来查找对象,那么 Map 会更加高效且易于使用(和维护)。

回答by Davide Lorenzo MARINO

If you know the position of the element do only the following:

如果您知道元素的位置,只需执行以下操作:

userList.get(index).setUsername("newvalue");

If not, you need to loop all the elements to find the element to update

如果不是,则需要循环所有元素,找到要更新的元素

for (User user : userList) {
    if (user.getUserId().equals(searchedId)) {
        user.setUsername("newvalue");
        break;  
    }
}

回答by i23

 for(User user : userList) {
    if(user.getId == 2) {
        user.setUsername("newUsername")
    }
}

回答by triadiktyo

If you need to just update the name then you do not need to create a new object and insert it into the list. As @Davide has pointed out you can iterate over the list and set the username.

如果您只需要更新名称,则无需创建新对象并将其插入列表中。正如@Davide 指出的,您可以遍历列表并设置用户名。

I would suggest on top of that, in the interest of efficiency, to use a Hashtable<Integer, User> userTablerather than a list to prevent the iteration over the whole list to find the right User

最重要的是,为了提高效率,我建议使用一个Hashtable<Integer, User> userTable而不是列表来防止遍历整个列表以找到合适的用户

This way you can get the user by its ID efficiently get set the username as shown here userTable.get(id).setUsername("new username")

通过这种方式,您可以通过其 ID 有效地获取用户 设置用户名,如下所示 userTable.get(id).setUsername("new username")