public boolean removeAll(Collection<?> c) {
return batchRemove(c, false, 0, size);//complement 为false
}
public boolean retainAll(Collection<?> c) {
return batchRemove(c, true, 0, size);//complement 为true
}
//complement 参数复用
boolean batchRemove(Collection<?> c, boolean complement,
final int from, final int end) {
Objects.requireNonNull(c);
final Object[] es = elementData;
int r;
// Optimize for initial run of survivors
for (r = from;; r++) {
if (r == end)
return false;
if (c.contains(es[r]) != complement)
//索引到ArryList的elementData集合中(不)包含c集合元素的 位置为r
break;
}
int w = r++;
try {
for (Object e; r < end; r++)
if (c.contains(e = es[r]) == complement)
es[w++] = e;
} catch (Throwable ex) {
// Preserve behavioral compatibility with AbstractCollection,
// even if c.contains() throws.
System.arraycopy(es, r, es, w, end - r);
w += end - r;
throw ex;
} finally {
modCount += end - w;
shiftTailOverGap(es, w, end);
}
return true;
}