/** * Returns the elements in an array. * * @return an array containing all set elements. */ @Override public Object[] toArray() { return isSizeKnown() ? super.toArray() : toArray(iterator(), new Object[32], true); }
/** * Returns the elements in an array. * * @return an array containing all set elements. */ @Override public Object[] toArray() { return isSizeKnown() ? super.toArray() : toArray(new Object[32], true); }
/** * Returns {@code true} if this set is empty. * This method avoids to invoke {@link #size()} unless it is cheap. * * @return {@code true} if this set is empty. */ @Override public boolean isEmpty() { return isSizeKnown() ? super.isEmpty() : !iterator().hasNext(); }
/** * Returns the number of elements in this set. The default implementation counts the number of elements * returned by the {@link #iterator() iterator}. Subclasses are encouraged to cache this value if they * know that the underlying storage is immutable. * * @return the number of elements in this set. */ @Override public int size() { int count = 0; for (final Iterator<E> it=iterator(); it.hasNext();) { it.next(); if (++count == Integer.MAX_VALUE) { break; } } return count; }
/** * Returns the elements in an array. * * @return an array containing all list elements. */ @Override public Object[] toArray() { return sizeIfKnown() >= 0 ? super.toArray() : SetOfUnknownSize.toArray(iterator(), new Object[32], true); }
/** * Removes elements of the given collection from this set. * This method avoids to invoke {@link #size()}. * * @param c the collection containing elements to remove. * @return {@code true} if at least one element has been removed. */ @Override public boolean removeAll(final Collection<?> c) { /* * Do not invoke super.removeAll(c) even if isSizeKnown() returns 'true' because we want to unconditionally * iterate over the elements of the given collection. The reason is that this Set may compute the values in * a dynamic way and it is sometime difficult to ensure that the values returned by this Set's iterator are * fully consistent with the values recognized by contains(Object) and remove(Object) methods. Furthermore * we want the operation to fail fast in the common case where the remove(Object) method is unsupported. */ boolean modified = false; for (final Iterator<?> it = c.iterator(); it.hasNext();) { modified |= remove(it.next()); } return modified; }
return super.contains(object);
/** * Returns {@code true} if this set is empty. * This method avoids to invoke {@link #size()} unless it is cheap. * * @return {@code true} if this set is empty. */ @Override public boolean isEmpty() { return isSizeKnown() ? super.isEmpty() : !iterator().hasNext(); }
/** * Returns the elements in the given array, or in a new array of the same type * if it was necessary to allocate more space. * * @param <T> the type array elements. * @param array where to store the elements. * @return an array containing all set elements. */ @Override @SuppressWarnings("SuspiciousToArrayCall") public <T> T[] toArray(final T[] array) { return isSizeKnown() ? super.toArray(array) : toArray(array, false); }
/** * Returns the number of elements in this set. The default implementation counts the number of elements * returned by the {@link #iterator() iterator}. Subclasses are encouraged to cache this value if they * know that the underlying storage is immutable. * * @return the number of elements in this set. */ @Override public int size() { int count = 0; for (final Iterator<E> it=iterator(); it.hasNext();) { it.next(); if (++count == Integer.MAX_VALUE) { break; } } return count; }
/** * Returns the elements in the given array, or in a new array of the same type * if it was necessary to allocate more space. * * @param <T> the type array elements. * @param array where to store the elements. * @return an array containing all list elements. */ @Override @SuppressWarnings("SuspiciousToArrayCall") public <T> T[] toArray(final T[] array) { return sizeIfKnown() >= 0 ? super.toArray(array) : SetOfUnknownSize.toArray(iterator(), array, false); }
/** * Removes elements of the given collection from this set. * This method avoids to invoke {@link #size()}. * * @param c the collection containing elements to remove. * @return {@code true} if at least one element has been removed. */ @Override public boolean removeAll(final Collection<?> c) { /* * Do not invoke super.removeAll(c) even if isSizeKnown() returns 'true' because we want to unconditionally * iterate over the elements of the given collection. The reason is that this Set may compute the values in * a dynamic way and it is sometime difficult to ensure that the values returned by this Set's iterator are * fully consistent with the values recognized by contains(Object) and remove(Object) methods. Furthermore * we want the operation to fail fast in the common case where the remove(Object) method is unsupported. */ boolean modified = false; for (final Iterator<?> it = c.iterator(); it.hasNext();) { modified |= remove(it.next()); } return modified; }
return super.contains(object);
/** * Returns the elements in the given array, or in a new array of the same type * if it was necessary to allocate more space. * * @param <T> the type array elements. * @param array where to store the elements. * @return an array containing all set elements. */ @Override @SuppressWarnings("SuspiciousToArrayCall") public <T> T[] toArray(final T[] array) { return isSizeKnown() ? super.toArray(array) : toArray(iterator(), array, false); }
/** * Creates a {@code Spliterator} without knowledge of collection size. * * @return a {@code Spliterator} over the elements in this collection. */ @Override public Spliterator<E> spliterator() { return isSizeKnown() ? super.spliterator() : Spliterators.spliteratorUnknownSize(iterator(), 0); }
for (final Iterator<E> it = iterator(); it.hasNext();) { if (!that.contains(it.next())) { return false;
for (final Iterator<E> it = iterator(); it.hasNext();) { if (!that.contains(it.next())) { return false;
/** * Implementation of the public {@code toArray()} methods. */ @SuppressWarnings("unchecked") private <T> T[] toArray(T[] array, boolean trimToSize) { int i = 0; for (final Iterator<E> it = iterator(); it.hasNext();) { if (i >= array.length) { if (i >= Integer.MAX_VALUE >>> 1) { throw new OutOfMemoryError("Required array size too large"); } array = Arrays.copyOf(array, Math.max(16, array.length) << 1); trimToSize = true; } array[i++] = (T) it.next(); // Will throw an ArrayStoreException if the type is incorrect. } if (trimToSize) { array = ArraysExt.resize(array, i); } else { Arrays.fill(array, i, array.length, null); } return array; }