更新时间:2023-11-25 22:40:22
如果我理解正确,您只想从 list1
中删除单个 2
元素而不是全部其中.您可以遍历 list2
并尝试从 list1
中删除每个元素.请记住,如果 list2
不能包含重复项,还有比这更有效的方法.
If I understand correctly, you only want to remove a single 2
element from list1
rather than all of them. You can iterate over list2
and attempt to remove each element from list1
. Keep in mind that there are more efficient methods than this if list2
cannot contain duplicates.
var list1 = new ArrayList<>(List.of(1, 2, 2));
var list2 = List.of(2, 3, 4);
list2.forEach(list1::remove);
list1
现在包含以下内容:
[1, 2]
请参阅 starman1979 的答案以获取相同的解决方案,但使用 lambda 而不是方法引用.
See starman1979's answer for the same solution, but using a lambda rather than a method reference.