Class ArraySet<E extends @UnknownSignedness Object>

Type Parameters:
E - the type of the set elements
All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>

public class ArraySet<E extends @UnknownSignedness Object> extends AbstractSet<E>
A set backed by an array. It permits null values and its iterator has deterministic ordering.

Compared to a HashSet or LinkedHashSet: For very small sets, this uses much less space, has comparable performance, and (like a LinkedHashSet) is deterministic, with elements returned in the order they were inserted. For large sets, this is significantly less performant than other set implementations.

Compared to a TreeSet: This uses somewhat less space, and it does not require defining a comparator. This isn't sorted but does have deterministic ordering. For large sets, this is significantly less performant than other set implementations.

Other ArraySet implementations include:

  • https://docs.oracle.com/en/database/oracle/oracle-database/19/olapi/oracle/olapi/ArraySet.html
  • https://developer.android.com/reference/android/util/ArraySet
  • https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/CopyOnWriteArraySet.html
All of those use the GPL or the Apache License, version 2.0, whereas this implementation is licensed under the more libral MIT License. In addition, some of those implementations forbid nulls or nondeterministically reorder the contents, and others don't specify their behavior regarding nulls and ordering. CopyOnWriteArraySet uses an ArrayList, not an array, as its representation.
  • Constructor Details

    • ArraySet

      @SideEffectFree public ArraySet(int initialCapacity)
      Constructs an empty ArraySet with the specified initial capacity.
      Parameters:
      initialCapacity - the initial capacity
      Throws:
      IllegalArgumentException - if the initial capacity is negative
    • ArraySet

      @SideEffectFree public ArraySet()
      Constructs an empty ArraySet with the default initial capacity.
    • ArraySet

      @SideEffectFree public ArraySet(Collection<? extends E> m)
      Constructs a new ArraySet with the same elements as the given collection.
      Parameters:
      m - the collection whose elements are to be placed in the new set
      Throws:
      NullPointerException - if the given set is null
  • Method Details

    • newArraySetOrHashSet

      public static <E> Set<E> newArraySetOrHashSet(int capacity)
      Returns a new ArraySet or HashSet with the given capacity. Uses an ArraySet if the capacity is small, and a HashSet otherwise.
      Type Parameters:
      E - the type of the elements
      Parameters:
      capacity - the expected maximum number of elements in the set
      Returns:
      a new ArraySet or HashSet with the given capacity
    • newArraySetOrHashSet

      public static <E> Set<E> newArraySetOrHashSet(Collection<E> s)
      Returns a new ArraySet or HashSet with the given elements. Uses an ArraySet if the capacity is small, and a HashSet otherwise.
      Type Parameters:
      E - the type of the elements
      Parameters:
      s - the elements to put in the returned set
      Returns:
      a new ArraySet or HashSet with the given elements
    • newArraySetOrLinkedHashSet

      public static <E> Set<E> newArraySetOrLinkedHashSet(int capacity)
      Returns a new ArraySet or LinkedHashSet with the given capacity. Uses an ArraySet if the capacity is small, and a LinkedHashSet otherwise.
      Type Parameters:
      E - the type of the elements
      Parameters:
      capacity - the expected maximum number of elements in the set
      Returns:
      a new ArraySet or LinkedHashSet with the given capacity
    • newArraySetOrLinkedHashSet

      public static <E> Set<E> newArraySetOrLinkedHashSet(Collection<E> s)
      Returns a new ArraySet or LinkedHashSet with the given elements. Uses an ArraySet if the capacity is small, and a LinkedHashSet otherwise.
      Type Parameters:
      E - the type of the elements
      Parameters:
      s - the elements to put in the returned set
      Returns:
      a new ArraySet or LinkedHashSet with the given elements
    • size

      @Pure public @org.checkerframework.checker.index.qual.NonNegative int size()
      Specified by:
      size in interface Collection<E extends @UnknownSignedness Object>
      Specified by:
      size in interface Set<E extends @UnknownSignedness Object>
      Specified by:
      size in class AbstractCollection<E extends @UnknownSignedness Object>
    • isEmpty

      @Pure public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<E extends @UnknownSignedness Object>
      Specified by:
      isEmpty in interface Set<E extends @UnknownSignedness Object>
      Overrides:
      isEmpty in class AbstractCollection<E extends @UnknownSignedness Object>
    • contains

      @Pure public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object value)
      Specified by:
      contains in interface Collection<E extends @UnknownSignedness Object>
      Specified by:
      contains in interface Set<E extends @UnknownSignedness Object>
      Overrides:
      contains in class AbstractCollection<E extends @UnknownSignedness Object>
    • add

      public boolean add(E value)
      Specified by:
      add in interface Collection<E extends @UnknownSignedness Object>
      Specified by:
      add in interface Set<E extends @UnknownSignedness Object>
      Overrides:
      add in class AbstractCollection<E extends @UnknownSignedness Object>
    • remove

      public boolean remove(@GuardSatisfied @Nullable @UnknownSignedness Object value)
      Specified by:
      remove in interface Collection<E extends @UnknownSignedness Object>
      Specified by:
      remove in interface Set<E extends @UnknownSignedness Object>
      Overrides:
      remove in class AbstractCollection<E extends @UnknownSignedness Object>
    • addAll

      public boolean addAll(Collection<? extends E> c)
      Specified by:
      addAll in interface Collection<E extends @UnknownSignedness Object>
      Specified by:
      addAll in interface Set<E extends @UnknownSignedness Object>
      Overrides:
      addAll in class AbstractCollection<E extends @UnknownSignedness Object>
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<E extends @UnknownSignedness Object>
      Specified by:
      removeAll in interface Set<E extends @UnknownSignedness Object>
      Overrides:
      removeAll in class AbstractSet<E extends @UnknownSignedness Object>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<E extends @UnknownSignedness Object>
      Specified by:
      clear in interface Set<E extends @UnknownSignedness Object>
      Overrides:
      clear in class AbstractCollection<E extends @UnknownSignedness Object>
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Collection<E extends @UnknownSignedness Object>
      Specified by:
      iterator in interface Iterable<E extends @UnknownSignedness Object>
      Specified by:
      iterator in interface Set<E extends @UnknownSignedness Object>
      Specified by:
      iterator in class AbstractCollection<E extends @UnknownSignedness Object>
    • forEach

      public void forEach(Consumer<? super E> action)
    • clone

      @SideEffectFree public ArraySet<E> clone()
      Returns a copy of this.
      Overrides:
      clone in class Object
      Returns:
      a copy of this
    • sort

      public void sort()
      Sorts the internal representation of this. Side-effects the representation, but not the abstract value, of this. Requires that the elements of this are comparable.
    • sort

      public void sort(Comparator<? super E> comparator)
      Sorts the internal representation of this, using the given comparator. Side-effects the representation, but not the abstract value, of this.
      Parameters:
      comparator - imposes an ordering on the elements of this