【发布时间】:2020-01-26 16:01:21
【问题描述】:
这是我的代码的相关部分:
List<List<String>> list2 = new ArrayList<>();
public void process(List<String> z) {
if (z.size() > 0) {
String x = z.get(0);
List<String> temp = new ArrayList<>();
z.stream().filter(e -> !e.equals(x)).forEach(e -> {
// some operations on temp
});
list2.add(temp); // adding temp to list2
z.removeIf(e -> temp.contains(e));
temp.clear(); // clearing temp
z.forEach(System.out::println);
list2.forEach((System.out::println)); // empty list2
process(z);
list2.forEach(e -> process(e));
}
在递归调用 process 之前,我必须清除 temp。
这里的问题是,我的list2 在清除temp 后变为空。
当我在 lambda 表达式中使用 temp 时,我无法将其重新分配为 null 或 new ArrayList<>()
(否则它会起作用)。
我想创建一个新列表并在临时列表和新列表之间复制,但感觉不是一个合适的方法。
有没有其他方法可以做到这一点?
【问题讨论】:
-
"我必须在递归调用
process之前清除 temp。" - 为什么? -
@DHS 没有理由。
temp引用同样存储在list2中的列表有什么影响? -
temp是一个局部变量。每个递归调用都有自己的temp副本。无需清空它。事实上,循环的每次迭代都会有一个新的temp-变量。 -
z.removeIf(e -> temp.contains(e))是z.removeAll(temp)的效率较低的版本。一般来说,你似乎有过度使用 lambda 表达式的习惯。 -
只知道 API 是不可能的……
标签: java arraylist collections garbage-collection