【发布时间】:2017-09-05 12:51:37
【问题描述】:
我有以下数组列表:
ArrayList<Obj o> list1 = new ArrayList<>();
ArrayList<String> list2 = new ArrayList<>();
我想从 list1 中删除所有(字符串)ID 等于 list2 中的元素的元素。
if(o.getId().equals(one of the strings from list2)) -> remove.
如何使用 removeAll 或其他方式来做到这一点,而无需编写额外的 for。我正在寻找最好的方法来做到这一点。
提前谢谢你。
【问题讨论】:
-
我认为即使 removeAll 在内部使用循环,所以我认为你不会比
for (Iterator it = list1.iterator(); it.hasNext(); ) { if (list2.contains(it.next().getId()) it.remove(); }更好