1.removeIf

    1. public boolean removeIf(Predicate<? super E> filter) {
    2. int removeCount = 0;
    3. // 位图
    4. final BitSet removeSet = new BitSet(size);
    5. final int expectedModCount = modCount;
    6. final int size = this.size;
    7. for (int i=0; modCount == expectedModCount && i < size; i++) {
    8. final E element = (E) elementData[i];
    9. // 条件满足
    10. if (filter.test(element)) {
    11. // 设置为true
    12. removeSet.set(i);
    13. removeCount++;
    14. }
    15. }
    16. if (modCount != expectedModCount) {
    17. throw new ConcurrentModificationException();
    18. }
    19. final boolean anyToRemove = removeCount > 0;
    20. if (anyToRemove) {
    21. final int newSize = size - removeCount;
    22. for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) {
    23. // 返回>=i的值为false的最近的下标
    24. i = removeSet.nextClearBit(i);
    25. elementData[j] = elementData[i];
    26. }
    27. for (int k=newSize; k < size; k++) {
    28. elementData[k] = null;
    29. }
    30. this.size = newSize;
    31. if (modCount != expectedModCount) {
    32. throw new ConcurrentModificationException();
    33. }
    34. modCount++;
    35. }
    36. return anyToRemove;
    37. }

    2.removeAll

    1. public boolean removeAll(Collection<?> c) {
    2. return batchRemove(c, false);
    3. }
    4. private boolean batchRemove(Collection<?> c, boolean complement) {
    5. final Object[] elementData = this.elementData;
    6. int r = 0, w = 0;
    7. boolean modified = false;
    8. try {
    9. for (; r < size; r++)
    10. if (c.contains(elementData[r]) == complement)
    11. elementData[w++] = elementData[r];
    12. } finally {
    13. if (w != size) {
    14. for (int i = w; i < size; i++)
    15. elementData[i] = null;
    16. modCount += size - w;
    17. size = w;
    18. modified = true;
    19. }
    20. }
    21. return modified;
    22. }

    3.iterator

    1. private class Itr implements Iterator<E> {
    2. int cursor;
    3. int lastRet = -1;
    4. int expectedModCount = modCount;
    5. Itr() {}
    6. public boolean hasNext() {
    7. return cursor != size;
    8. }
    9. public E next() {
    10. checkForComodification();
    11. int i = cursor;
    12. if (i >= size)
    13. throw new NoSuchElementException();
    14. Object[] elementData = ArrayList.this.elementData;
    15. if (i >= elementData.length)
    16. throw new ConcurrentModificationException();
    17. cursor = i + 1;
    18. return (E) elementData[lastRet = i];
    19. }
    20. public void remove() {
    21. if (lastRet < 0)
    22. throw new IllegalStateException();
    23. checkForComodification();
    24. try {
    25. ArrayList.this.remove(lastRet);
    26. cursor = lastRet;
    27. lastRet = -1;
    28. expectedModCount = modCount;
    29. } catch (IndexOutOfBoundsException ex) {
    30. throw new ConcurrentModificationException();
    31. }
    32. }
    33. final void checkForComodification() {
    34. if (modCount != expectedModCount)
    35. throw new ConcurrentModificationException();
    36. }
    37. }

    4.从后往前遍历

    1. List<Long> list = new ArrayList<>(Arrays.asList(1L,2L,2L,4L,5L));
    2. for (int i = list.size() - 1; i >= 0; i--) {
    3. if (list.get(i) == 2) {
    4. list.remove(i);
    5. }
    6. }

    5.i—

    1. List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L, 2L, 4L, 5L));
    2. for (int i = 0; i < list.size(); i++) {
    3. if (list.get(i) == 2) {
    4. list.remove(i);
    5. i--;
    6. }
    7. }