/** * 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 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; }
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; }
/** * 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(); }
/** * 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); }
/** * 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 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 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); }