Class RedBlackSet<E>

java.lang.Object
org.jhotdraw8.icollection.RedBlackSet<E>
Type Parameters:
E - the element type
All Implemented Interfaces:
Serializable, Iterable<E>, ImmutableCollection<E>, ImmutableNavigableSet<E>, ImmutableSet<E>, ImmutableSortedSet<E>, ReadOnlyCollection<E>, ReadOnlyNavigableSet<E>, ReadOnlySequencedCollection<E>, ReadOnlySequencedSet<E>, ReadOnlySet<E>, ReadOnlySortedSet<E>

public class RedBlackSet<E> extends Object implements ImmutableNavigableSet<E>, Serializable
Implements the ImmutableNavigableSet interface using a Red-Black tree.

References:

For a similar design, see 'TreeSet.java' in vavr. The internal data structure of this class is licensed from vavr.

TreeSet.java. Copyright 2023 (c) vavr. MIT License.
github.com
See Also:
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    RedBlackSet(PrivateData privateData)
    Creates a new instance with the provided privateData data object.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(E element)
    Returns a copy of this set that contains all elements of this set and also the specified element.
    addAll(Iterable<? extends E> c)
    Returns a copy of this set that contains all elements of this set and also all elements of the specified collection.
    @Nullable E
    Returns the least element in this set greater than or equal to the given element, or null if there is no such element.
    @Nullable Comparator<? super E>
    Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
    boolean
    Returns true if this collection contains the specified object.
    static <E> RedBlackSet<E>
    copyOf(Iterable<? extends E> c)
    Returns an immutable set that contains the provided elements sorted according to the natural ordering of its elements.
    static <E> RedBlackSet<E>
    copyOf(@Nullable Comparator<E> comparator, Iterable<? extends E> c)
    Returns an immutable set that contains the provided elements, sorted according to the specified comparator.
    <T> RedBlackSet<T>
    Returns an empty set instance that has the specified element type.
    empty(@Nullable Comparator<T> comparator)
    Returns a copy of this collection that is empty, and has the specified type and comparator.
    boolean
    equals(@Nullable Object other)
    Compares the specified object with this set for equality.
    @Nullable E
    floor(E e)
    Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
    Gets the first element.
    Gets the last element.
    int
    Returns the hash code value for this set.
    @Nullable E
    higher(E e)
    Returns the least element in this set greater than the given element, or null if there is no such element.
    Returns an iterator over the elements in this collection.
    @Nullable E
    lower(E e)
    Returns the greatest element in this set less than the given element, or null if there is no such element.
    int
    Returns the maximal number of elements that this collection type can hold
    protected RedBlackSet<E>
    newInstance(PrivateData privateData)
    Creates a new instance with the provided privateData object as its internal data structure.
    static <E> RedBlackSet<E>
    of()
    Returns an empty immutable set, sorted according to the natural ordering of its elements.
    static <E> RedBlackSet<E>
    of(E @Nullable ... elements)
    Returns an immutable set that contains the provided elements, sorted according to the natural ordering of its elements.
    Returns a reversed-order view of this set.
    remove(E element)
    Returns a copy of this set that contains all elements of this set except the specified element.
    Returns a copy of this set that contains all elements of this set except the elements of the specified collection.
    Returns a copy of this set that contains only elements that are in this set and in the specified collection.
    int
    Returns the size of the collection.
    static <E> RedBlackSet<E>
    sortedOf(@Nullable Comparator<E> comparator)
    Returns an empty immutable set, sorted according to the specified comparator.
    static <E> RedBlackSet<E>
    sortedOf(@Nullable Comparator<E> comparator, E @Nullable ... elements)
    Returns an immutable set that contains the provided elements, sorted according to the specified comparator.
     
    Returns a mutable copy of this set.
     

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface org.jhotdraw8.icollection.immutable.ImmutableCollection

    filter

    Methods inherited from interface org.jhotdraw8.icollection.immutable.ImmutableNavigableSet

    removeFirst, removeLast, reversed

    Methods inherited from interface java.lang.Iterable

    forEach

    Methods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlyCollection

    containsAll, isEmpty, stream, toArray, toArray

    Methods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySequencedCollection

    asCollection

    Methods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySequencedSet

    asSet

    Methods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySet

    characteristics
  • Constructor Details

    • RedBlackSet

      protected RedBlackSet(PrivateData privateData)
      Creates a new instance with the provided privateData data object.

      This constructor is intended to be called from a constructor of the subclass, that is called from method newInstance(PrivateData).

      Parameters:
      privateData - an privateData data object
  • Method Details

    • newInstance

      protected RedBlackSet<E> newInstance(PrivateData privateData)
      Creates a new instance with the provided privateData object as its internal data structure.

      Subclasses must override this method, and return a new instance of their subclass!

      Parameters:
      privateData - the internal data structure needed by this class for creating the instance.
      Returns:
      a new instance of the subclass
    • copyOf

      public static <E> RedBlackSet<E> copyOf(@Nullable Comparator<E> comparator, Iterable<? extends E> c)
      Returns an immutable set that contains the provided elements, sorted according to the specified comparator.
      Type Parameters:
      E - the element type
      Parameters:
      comparator - a comparator, if null the natural ordering of the elements is used
      c - an iterable
      Returns:
      an immutable set of the provided elements
    • copyOf

      public static <E> RedBlackSet<E> copyOf(Iterable<? extends E> c)
      Returns an immutable set that contains the provided elements sorted according to the natural ordering of its elements.
      Type Parameters:
      E - the element type
      Parameters:
      c - an iterable
      Returns:
      an immutable set of the provided elements
    • sortedOf

      public static <E> RedBlackSet<E> sortedOf(@Nullable Comparator<E> comparator)
      Returns an empty immutable set, sorted according to the specified comparator.
      Type Parameters:
      E - the element type
      Parameters:
      comparator - a comparator, if null the natural ordering of the elements is used
      Returns:
      an empty immutable set
    • sortedOf

      @SafeVarargs public static <E> RedBlackSet<E> sortedOf(@Nullable Comparator<E> comparator, E @Nullable ... elements)
      Returns an immutable set that contains the provided elements, sorted according to the specified comparator.
      Type Parameters:
      E - the element type
      Parameters:
      comparator - a comparator, if null the natural ordering of the elements is used
      elements - elements
      Returns:
      an immutable set of the provided elements
    • of

      public static <E> RedBlackSet<E> of()
      Returns an empty immutable set, sorted according to the natural ordering of its elements.
      Type Parameters:
      E - the element type
      Returns:
      an empty immutable set
    • of

      @SafeVarargs public static <E> RedBlackSet<E> of(E @Nullable ... elements)
      Returns an immutable set that contains the provided elements, sorted according to the natural ordering of its elements.
      Type Parameters:
      E - the element type
      Parameters:
      elements - elements
      Returns:
      an iterable of elements
    • add

      public RedBlackSet<E> add(E element)
      Description copied from interface: ImmutableSet
      Returns a copy of this set that contains all elements of this set and also the specified element.
      Specified by:
      add in interface ImmutableCollection<E>
      Specified by:
      add in interface ImmutableNavigableSet<E>
      Specified by:
      add in interface ImmutableSet<E>
      Specified by:
      add in interface ImmutableSortedSet<E>
      Parameters:
      element - an element
      Returns:
      this set instance if it already contains the element, or a different set instance with the element added
    • getFirst

      public E getFirst()
      Description copied from interface: ReadOnlySequencedCollection
      Gets the first element.
      Specified by:
      getFirst in interface ReadOnlySequencedCollection<E>
      Returns:
      an element
    • getLast

      public E getLast()
      Description copied from interface: ReadOnlySequencedCollection
      Gets the last element.
      Specified by:
      getLast in interface ReadOnlySequencedCollection<E>
      Returns:
      an element
    • addAll

      public RedBlackSet<E> addAll(Iterable<? extends E> c)
      Description copied from interface: ImmutableSet
      Returns a copy of this set that contains all elements of this set and also all elements of the specified collection.
      Specified by:
      addAll in interface ImmutableCollection<E>
      Specified by:
      addAll in interface ImmutableNavigableSet<E>
      Specified by:
      addAll in interface ImmutableSet<E>
      Specified by:
      addAll in interface ImmutableSortedSet<E>
      Parameters:
      c - a collection to be added to this set
      Returns:
      this set instance if it already contains the elements, or a different set instance with the elements added
    • ceiling

      public @Nullable E ceiling(E e)
      Description copied from interface: ReadOnlyNavigableSet
      Returns the least element in this set greater than or equal to the given element, or null if there is no such element.
      Specified by:
      ceiling in interface ReadOnlyNavigableSet<E>
      Parameters:
      e - the given element
      Returns:
      ceiling element or null
    • empty

      public <T> RedBlackSet<T> empty()
      Description copied from interface: ImmutableSet
      Returns an empty set instance that has the specified element type.
      Specified by:
      empty in interface ImmutableCollection<E>
      Specified by:
      empty in interface ImmutableNavigableSet<E>
      Specified by:
      empty in interface ImmutableSet<E>
      Specified by:
      empty in interface ImmutableSortedSet<E>
      Type Parameters:
      T - the element type of the returned set
      Returns:
      an empty set of the specified element type.
    • empty

      public <T> ImmutableCollection<T> empty(@Nullable Comparator<T> comparator)
      Description copied from interface: ImmutableSortedSet
      Returns a copy of this collection that is empty, and has the specified type and comparator.
      Specified by:
      empty in interface ImmutableSortedSet<E>
      Type Parameters:
      T - the element type of the collection
      Parameters:
      comparator - a comparator for ordering the elements of the set, specify null to use the natural order of the elements
      Returns:
      an empty collection of the specified type and comparator
    • comparator

      public @Nullable Comparator<? super E> comparator()
      Description copied from interface: ReadOnlySortedSet
      Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
      Specified by:
      comparator in interface ReadOnlySortedSet<E>
      Returns:
      comparator or null
    • contains

      public boolean contains(Object o)
      Description copied from interface: ReadOnlyCollection
      Returns true if this collection contains the specified object.
      Specified by:
      contains in interface ReadOnlyCollection<E>
      Parameters:
      o - an object
      Returns:
      true if this collection contains the specified object
    • floor

      public @Nullable E floor(E e)
      Description copied from interface: ReadOnlyNavigableSet
      Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
      Specified by:
      floor in interface ReadOnlyNavigableSet<E>
      Parameters:
      e - the given element
      Returns:
      floor element or null
    • higher

      public @Nullable E higher(E e)
      Description copied from interface: ReadOnlyNavigableSet
      Returns the least element in this set greater than the given element, or null if there is no such element.
      Specified by:
      higher in interface ReadOnlyNavigableSet<E>
      Parameters:
      e - the given element
      Returns:
      higher element or null
    • iterator

      public Iterator<E> iterator()
      Description copied from interface: ReadOnlyCollection
      Returns an iterator over the elements in this collection.
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface ReadOnlyCollection<E>
      Returns:
      an iterator
    • spliterator

      public Spliterator<E> spliterator()
      Specified by:
      spliterator in interface Iterable<E>
    • lower

      public @Nullable E lower(E e)
      Description copied from interface: ReadOnlyNavigableSet
      Returns the greatest element in this set less than the given element, or null if there is no such element.
      Specified by:
      lower in interface ReadOnlyNavigableSet<E>
      Parameters:
      e - the given element
      Returns:
      lower element or null
    • maxSize

      public int maxSize()
      Description copied from interface: ImmutableCollection
      Returns the maximal number of elements that this collection type can hold
      Specified by:
      maxSize in interface ImmutableCollection<E>
      Returns:
      the maximal size
    • readOnlyReversed

      public ReadOnlySequencedSet<E> readOnlyReversed()
      Description copied from interface: ReadOnlySequencedSet
      Returns a reversed-order view of this set. Changes to the underlying set are visible in the reversed view.
      Specified by:
      readOnlyReversed in interface ReadOnlySequencedCollection<E>
      Specified by:
      readOnlyReversed in interface ReadOnlySequencedSet<E>
      Returns:
      a reversed-order view of this set
    • remove

      public RedBlackSet<E> remove(E element)
      Description copied from interface: ImmutableSet
      Returns a copy of this set that contains all elements of this set except the specified element.
      Specified by:
      remove in interface ImmutableCollection<E>
      Specified by:
      remove in interface ImmutableNavigableSet<E>
      Specified by:
      remove in interface ImmutableSet<E>
      Specified by:
      remove in interface ImmutableSortedSet<E>
      Parameters:
      element - an element
      Returns:
      this set instance if it already does not contain the element, or a different set instance with the element removed
    • removeAll

      public RedBlackSet<E> removeAll(Iterable<?> c)
      Description copied from interface: ImmutableSet
      Returns a copy of this set that contains all elements of this set except the elements of the specified collection.
      Specified by:
      removeAll in interface ImmutableCollection<E>
      Specified by:
      removeAll in interface ImmutableNavigableSet<E>
      Specified by:
      removeAll in interface ImmutableSet<E>
      Specified by:
      removeAll in interface ImmutableSortedSet<E>
      Parameters:
      c - a collection with elements to be removed from this set
      Returns:
      this set instance if it already does not contain the elements, or a different set instance with the elements removed
    • retainAll

      public RedBlackSet<E> retainAll(Iterable<?> c)
      Description copied from interface: ImmutableSet
      Returns a copy of this set that contains only elements that are in this set and in the specified collection.
      Specified by:
      retainAll in interface ImmutableCollection<E>
      Specified by:
      retainAll in interface ImmutableNavigableSet<E>
      Specified by:
      retainAll in interface ImmutableSet<E>
      Specified by:
      retainAll in interface ImmutableSortedSet<E>
      Parameters:
      c - a collection with elements to be retained in this set
      Returns:
      this set instance if it has not changed, or a different set instance with elements removed
    • size

      public int size()
      Description copied from interface: ReadOnlyCollection
      Returns the size of the collection.
      Specified by:
      size in interface ReadOnlyCollection<E>
      Returns:
      the size
    • hashCode

      public int hashCode()
      Description copied from interface: ReadOnlySet
      Returns the hash code value for this set. The hash code is the sum of the hash code of its elements.

      Implementations of this method should use ReadOnlySet.iteratorToHashCode(java.util.Iterator<E>).

      Specified by:
      hashCode in interface ReadOnlySet<E>
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this set
      See Also:
    • equals

      public boolean equals(@Nullable Object other)
      Description copied from interface: ReadOnlySet
      Compares the specified object with this set for equality.

      Returns true if the given object is also a read-only set and the two sets contain the same elements, ignoring the sequence of the elements.

      Implementations of this method should use ReadOnlySet.setEquals(org.jhotdraw8.icollection.readonly.ReadOnlySet<E>, java.lang.Object).

      Specified by:
      equals in interface ReadOnlySet<E>
      Overrides:
      equals in class Object
      Parameters:
      other - an object
      Returns:
      true if the object is equal to this map
    • toMutable

      public MutableRedBlackSet<E> toMutable()
      Description copied from interface: ImmutableSet
      Returns a mutable copy of this set.
      Specified by:
      toMutable in interface ImmutableCollection<E>
      Specified by:
      toMutable in interface ImmutableNavigableSet<E>
      Specified by:
      toMutable in interface ImmutableSet<E>
      Specified by:
      toMutable in interface ImmutableSortedSet<E>
      Returns:
      a mutable copy.
    • toString

      public String toString()
      Overrides:
      toString in class Object