[英]Java List removing items without any action
I'm facing a weird behavior in my Java code using List.我在使用 List 的 Java 代码中遇到了一个奇怪的行为。
The code is very simple, I have a List of Object called AccessRequest
which comes from a database and I'm using this first List to create a new one but with a filter to select only a few objects.代码非常简单,我有一个名为
AccessRequest
的 Object 列表,它来自数据库,我正在使用第一个列表创建一个新列表,但过滤器为 select 只有几个对象。
Here is the code:这是代码:
private void updateCommentIfNeeded() {
List<AccessRequest> accessRequestList = getAllRequest();
List<AccessRequest> commentsList = getCommentsListProcessedManually(accessRequestList);
}
public List<AccessRequest> getCommentsListProcessedManually(List<AccessRequest> accessRequests) {
accessRequests.removeIf(ar -> !ar.getComment().equals("To be processed manually"));
if (accessRequests.size() != 0) {
SQLServerConnection sqlServerConnection = new SQLServerConnection(sqlServerUrl);
accessRequests.removeIf(ar -> !sqlServerConnection.emailExists(ar.getEmail()));
}
return accessRequests;
}
I'm supposed to get a second List only containing the objects that has their comments to To be processed manually
, which I do.我应该得到第二个列表,其中仅包含对其注释为
To be processed manually
的对象,我这样做了。 But the weird part is that the first List also takes the value of the second as if I wrote accessRequestList = commentsList
but there is no such thing and I'm using local variable.但奇怪的是,第一个 List 也采用第二个的值,就好像我写了
accessRequestList = commentsList
但没有这样的东西,我使用的是局部变量。
Ex:前任:
I'm kind of lost here if anyone has an idea !如果有人有想法,我有点迷路了!
Your method getCommentsListProcessedManually
modifies the list you're passing.您的方法
getCommentsListProcessedManually
修改您传递的列表。 I believe you're operating under the assumption that passing the list as a parameter somehow creates a copy of the list, whereas what is actually happening is that a reference to the list is passed by value.我相信您的操作是假设将列表作为参数传递会以某种方式创建列表的副本,而实际发生的是对列表的引用是按值传递的。
There are several ways to solve this, but the easiest is to simply create a copy of your input list at the start of your method:有几种方法可以解决这个问题,但最简单的方法是在方法开始时简单地创建输入列表的副本:
public List<AccessRequest> getCommentsListProcessedManually(List<AccessRequest> input) {
List<AccessRequest> accessRequests = new ArrayList<>(input);
accessRequests.removeIf(ar -> !ar.getComment().equals("To be processed manually"));
if (accessRequests.size() != 0) {
SQLServerConnection sqlServerConnection = new SQLServerConnection(sqlServerUrl);
accessRequests.removeIf(ar -> !sqlServerConnection.emailExists(ar.getEmail()));
}
return accessRequests;
}
You could also use the Stream API for this (using the filter
operation), but that's quite a bit trickier in this situation.您也可以为此使用 Stream API(使用
filter
操作),但在这种情况下这有点棘手。
You are passing a reference of the list to the method getCommentsListProcessedManually.您正在将列表的引用传递给方法 getCommentsListProcessedManually。
So accessRequestList and the one passed as a parameter are the same, hence any operation done to the list is done to the same list.因此 accessRequestList 和作为参数传递的那个是相同的,因此对列表进行的任何操作都是对同一个列表进行的。
You can create a copy of the list before passing it as a parameter:您可以在将列表作为参数传递之前创建列表的副本:
List<AccessRequest> newList = new ArrayList<AccessRequest>(accessRequestList);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.