# Class SetOfUnknownSize<E>

Object
AbstractCollection<E>
AbstractSet<E>
SetOfUnknownSize<E>

Type Parameters:
  `E` - the type of elements in the set.

All Implemented Interfaces:
  `Iterable<E>, Collection<E>, Set<E>`

public abstract class SetOfUnknownSize<E> extends AbstractSet<E>
An alternative to `AbstractSet` for implementations having a costly `size()` method. The size of the set is not known in advance, but may become known after one complete iteration. This class overrides methods in a way that reduces or eliminates the need to invoke `size()`.

#### Note for implementers

Subclasses should implement the following methods:

- `AbstractCollection​.iterator()`
- `sizeIfKnown()` — optional but recommended for efficiency
- `characteristics()` — if the implementation disallows null elements.

Since:
  1.6

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `SetOfUnknownSize()`
  Creates a new set of initially unknown size.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected int`
  `characteristics()`
  Returns characteristics of this collection as a combination of `Spliterator` bits.
  `boolean`
  `equals(Object other)`
  Returns `true` if the given object is also a set and the two sets have the same content.
  `boolean`
  `isEmpty()`
  Returns `true` if this set is empty.
  `boolean`
  `removeAll(Collection<?> c)`
  Removes elements of the given collection from this set.
  `int`
  `size()`
  Returns the number of elements in this set.
  `protected OptionalInt`
  `sizeIfKnown()`
  Returns the `size()` value if it is already known, or empty if the size is still unknown.
  `Spliterator<E>`
  `spliterator()`
  Creates a `Spliterator` which may be of unknown size.
  `Object[]`
  `toArray()`
  Returns all elements in an array.
  `<T> T[]`
  `toArray(IntFunction<T[]> generator)`
  Returns the elements in an array generated by the given function.
  `<T> T[]`
  `toArray(T[] array)`
  Returns the elements in the given array, or in a new array if it was necessary to allocate more space.

  ### Methods inherited from class AbstractSet

  `hashCode`

  ### Methods inherited from class AbstractCollection

  `add, addAll, clear, contains, containsAll, iterator, remove, retainAll, toString`

  ### Methods inherited from class Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface Collection

  `parallelStream, removeIf, stream`

  ### Methods inherited from interface Iterable

  `forEach`

  ### Methods inherited from interface Set

  `add, addAll, clear, contains, containsAll, iterator, remove, retainAll`

- ## Constructor Details

  - ### SetOfUnknownSize

    protected SetOfUnknownSize()
    Creates a new set of initially unknown size.

- ## Method Details

  - ### isEmpty

    public boolean isEmpty()
    Returns `true` if this set is empty. The implementation of this method avoids to invoke `size()`.
    Specified by:
      `isEmpty` in interface `Collection<E>`
    Specified by:
      `isEmpty` in interface `Set<E>`
    Overrides:
      `isEmpty` in class `AbstractCollection<E>`
    Returns:
      `true` if this set is empty.

  - ### size

    public int size()
    Returns the number of elements in this set. The default implementation returns the size if known, or otherwise counts the number of elements returned by the iterator.
    Specified by:
      `size` in interface `Collection<E>`
    Specified by:
      `size` in interface `Set<E>`
    Specified by:
      `size` in class `AbstractCollection<E>`
    Returns:
      the number of elements in this set.

  - ### sizeIfKnown

    protected OptionalInt sizeIfKnown()
    Returns the `size()` value if it is already known, or empty if the size is still unknown. The default implementation always returns an empty value. Subclasses can overwrite this method if they have already computed and cached the number of elements. When this information is known, some `SetOfUnknownSize` methods will take a more efficient path.
    Returns:
      `size()` if its value is already known, or empty if this value is still costly to compute.

  - ### removeAll

    public boolean removeAll(Collection<?> c)
    Removes elements of the given collection from this set. The implementation of this method avoids to invoke `size()`.
    Specified by:
      `removeAll` in interface `Collection<E>`
    Specified by:
      `removeAll` in interface `Set<E>`
    Overrides:
      `removeAll` in class `AbstractSet<E>`
    Parameters:
      `c` - the collection containing elements to remove.
    Returns:
      `true` if at least one element has been removed.

  - ### characteristics

    protected int characteristics()
    Returns characteristics of this collection as a combination of `Spliterator` bits. The default implementation returns `Spliterator​.DISTINCT`. Subclasses should add the `Spliterator​.NONNULL` bit if they guarantee that this set does not contain the `null` element.
    Returns:
      the characteristics of this collection.
    See Also:
      - `Spliterator​.DISTINCT`
      - `Spliterator​.NONNULL`
      - `Spliterator​.IMMUTABLE`
      - `Spliterator​.characteristics()`

  - ### spliterator

    public Spliterator<E> spliterator()
    Creates a `Spliterator` which may be of unknown size.
    Returns:
      a `Spliterator` over the elements in this collection.

  - ### toArray

    public Object[] toArray()
    Returns all elements in an array. The implementation of this method avoids to invoke `size()`.
    Specified by:
      `toArray` in interface `Collection<E>`
    Specified by:
      `toArray` in interface `Set<E>`
    Overrides:
      `toArray` in class `AbstractCollection<E>`
    Returns:
      an array containing all set elements.

  - ### toArray

    public <T> T[] toArray(T[] array)
    Returns the elements in the given array, or in a new array if it was necessary to allocate more space. If the given array is larger than necessary, the remaining array elements are set to `null`. The implementation of this method avoids to invoke `size()`.
    Specified by:
      `toArray` in interface `Collection<E>`
    Specified by:
      `toArray` in interface `Set<E>`
    Overrides:
      `toArray` in class `AbstractCollection<E>`
    Type Parameters:
      `T` - the type array elements.
    Parameters:
      `array` - where to store the elements.
    Returns:
      an array containing all set elements.

  - ### toArray

    public <T> T[] toArray(IntFunction<T[]> generator)
    Returns the elements in an array generated by the given function. The implementation of this method avoids to invoke `size()`.
    Type Parameters:
      `T` - the type array elements.
    Parameters:
      `generator` - the function for allocating a new array.
    Returns:
      an array containing all set elements.

  - ### equals

    public boolean equals(Object other)
    Returns `true` if the given object is also a set and the two sets have the same content. The implementation of this method avoids to invoke `size()` on this instance. However, it still call `other​.size()`.
    Specified by:
      `equals` in interface `Collection<E>`
    Specified by:
      `equals` in interface `Set<E>`
    Overrides:
      `equals` in class `AbstractSet<E>`
    Parameters:
      `other` - the object to compare with this set.
    Returns:
      `true` if the two set have the same content.