listA.retainAll(listB);
// listA now contains only the elements which are also contained in listB.
如果您想避免 listA 中的更改受到影响,那么您需要创建一个新的。
List<Integer> common = new ArrayList<Integer>(listA);
common.retainAll(listB);
// common now contains only the elements which are contained in listA and listB.
使用
Collection#retainAll()
。如果您想避免
listA
中的更改受到影响,那么您需要创建一个新的。