code: private int removeOrRetain(Collection<?> collection, boolean retain, int from, int to) {
          for (int i = from; i < to; i++) {
              if (collection.contains(elements[i]) == retain) {
                  continue;
              }
  
              /*
               * We've encountered an element that must be removed! Create a new
               * array and copy in the surviving elements one by one.
               */
              Object[] newElements = new Object[elements.length - 1];
              System.arraycopy(elements, 0, newElements, 0, i);
              int newSize = i;
              for (int j = i + 1; j < to; j++) {
                  if (collection.contains(elements[j]) == retain) {
                      newElements[newSize++] = elements[j];
                  }
              }
  
              /*
               * Copy the elements after 'to'. This is only useful for sub lists,
               * where 'to' will be less than elements.length.
               */
              System.arraycopy(elements, to, newElements, newSize, elements.length - to);
              newSize += (elements.length - to);
  
              if (newSize < newElements.length) {
                  newElements = Arrays.copyOfRange(newElements, 0, newSize); // trim to size
              }
              int removed = elements.length - newElements.length;
              elements = newElements;
              return removed;
          }
  
          // we made it all the way through the loop without making any changes
          return 0;
      }
docs: Removes or retains the elements in {@code collection}. Returns the number
  of elements removed.