Type Parameters:
E - type of elements in this set
All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>
Direct Known Subclasses:
UnmodifiableSortedSet

public class ArraySet<E>extends AbstractSet<E> implements Set<E>
A small, fast Set with a maximum size, backed by a fixed-size array. Much more space-efficient than HashSet. Unsynchronized, not thread-safe. Null elements are not permitted.

Not appropriate for large Sets that are modified. add(), remove(Object), and contains() are O(n). Warning: addAll() and the Collection constructor are O(n**2).

  • Field Details

    • MAX_CAPACITY

      public static final int MAX_CAPACITY
      The maximum capacity for this set.
      See Also:
    • _entries

      protected final Object[] _entries
      The underlying array of entries.
    • _throwOnFull

      private final boolean _throwOnFull
      Whether to throw on overflow.
    • _size

      private int _size
      The current size of the set.
    • _overflowIndex

      private int _overflowIndex
      The overflow index for overwrite mode.
    • modCount

      private transient int modCount
      The modification count for fail-fast iterators.
  • Constructor Details

    • ArraySet

      public ArraySet()
      A fixed capacity of MAX_CAPACITY. Adds over capacity will throw a SetFullException.
    • ArraySet

      public ArraySet(Set<? extends E> c)
      A fixed capacity of max(MAX_CAPACITY, c.size()) Adds over capacity will throw a SetFullException.
      Parameters:
      c - the collection to copy
    • ArraySet

      public ArraySet(Set<? extends E> c, int capacity)
      A fixed capacity of max(capacity, c.size()) Adds over capacity will throw a SetFullException.
      Parameters:
      c - the collection to copy
      capacity - the maximum capacity
    • ArraySet

      public ArraySet(Collection<? extends E> c)
      A fixed capacity of max(MAX_CAPACITY, c.size()), which may be more than the resulting set size if there are duplicates in c. Adds over capacity will throw a SetFullException.

      Warning: O(n**2).

      Parameters:
      c - the collection to copy
    • ArraySet

      public ArraySet(Collection<? extends E> c, int capacity)
      A fixed capacity of max(capacity, c.size()), which may be more than the resulting set size if there are duplicates in c. Adds over capacity will throw a SetFullException.

      Warning: O(n**2).

      Parameters:
      c - the collection to copy
      capacity - the maximum capacity
    • ArraySet

      public ArraySet(E[] arr)
      A fixed capacity of arr.length. Adds over capacity will throw a SetFullException. arr must not contain duplicates, no checks are done. arr may contain nulls but they must be at the end.
      Parameters:
      arr - the array to copy
    • ArraySet

      public ArraySet(int capacity)
      Adds over capacity will throw a SetFullException.
      Parameters:
      capacity - the maximum size
      Throws:
      IllegalArgumentException - if capacity less than 1.
    • ArraySet

      public ArraySet(int capacity, boolean throwOnFull)
      If throwOnFull is false, adds over capacity will overwrite starting at slot zero. This breaks the AbstractCollection invariant that "a Collection will always contain the specified element after add() returns", but it prevents unexpected exceptions. If throwOnFull is true, adds over capacity will throw a SetFullException.
      Parameters:
      capacity - the maximum size
      throwOnFull - whether to throw on overflow
      Throws:
      IllegalArgumentException - if capacity less than 1.
  • Method Details