@Override public boolean retainAll(final Collection<?> c) { if (size == 0) return false; @SuppressWarnings("all") final CountSet retainIndices = new CountSet(c.size() + 2); retainIndices.add(-1); retainIndices.add(size); for (final Object o : c) { if (!(o instanceof Integer)) continue; final int pos = Arrays.binarySearch(elements,0,size,(int) o); if (pos < 0) continue; retainIndices.add(pos); } if (retainIndices.size == 2) { size = 0; return true; } else if (retainIndices.size == size + 2) { return false; } else { for (int idx = retainIndices.size - 1; idx > 0; idx--) { final int toIdx = retainIndices.elements[idx]; final int fromIdx = retainIndices.elements[idx - 1] + 1; removeIndices(toIdx,fromIdx); } return true; } }