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>
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
ConstructorsModifierConstructorDescriptionprotectedRedBlackSet(PrivateData privateData) Creates a new instance with the provided privateData data object. -
Method Summary
Modifier and TypeMethodDescriptionReturns a copy of this set that contains all elements of this set and also the specified element.Returns a copy of this set that contains all elements of this set and also all elements of the specified collection.@Nullable EReturns 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, ornullif this set uses the natural ordering of its elements.booleanReturnstrueif this collection contains the specified object.static <E> RedBlackSet<E> 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> empty()Returns an empty set instance that has the specified element type.<T> ImmutableCollection<T> empty(@Nullable Comparator<T> comparator) Returns a copy of this collection that is empty, and has the specified type and comparator.booleanCompares the specified object with this set for equality.@Nullable EReturns the greatest element in this set less than or equal to the given element, or null if there is no such element.getFirst()Gets the first element.getLast()Gets the last element.inthashCode()Returns the hash code value for this set.@Nullable EReturns the least element in this set greater than the given element, or null if there is no such element.iterator()Returns an iterator over the elements in this collection.@Nullable EReturns the greatest element in this set less than the given element, or null if there is no such element.intmaxSize()Returns the maximal number of elements that this collection type can holdprotected 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.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.intsize()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.toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.jhotdraw8.icollection.immutable.ImmutableCollection
filterMethods inherited from interface org.jhotdraw8.icollection.immutable.ImmutableNavigableSet
removeFirst, removeLast, reversedMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlyCollection
containsAll, isEmpty, stream, toArray, toArrayMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySequencedCollection
asCollectionMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySequencedSet
asSetMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySet
characteristics
-
Constructor Details
-
RedBlackSet
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
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, ifnullthe natural ordering of the elements is usedc- an iterable- Returns:
- an immutable set of the provided elements
-
copyOf
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
Returns an empty immutable set, sorted according to the specified comparator.- Type Parameters:
E- the element type- Parameters:
comparator- a comparator, ifnullthe 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, ifnullthe natural ordering of the elements is usedelements- elements- Returns:
- an immutable set of the provided elements
-
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
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
Description copied from interface:ImmutableSetReturns a copy of this set that contains all elements of this set and also the specified element.- Specified by:
addin interfaceImmutableCollection<E>- Specified by:
addin interfaceImmutableNavigableSet<E>- Specified by:
addin interfaceImmutableSet<E>- Specified by:
addin interfaceImmutableSortedSet<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
Description copied from interface:ReadOnlySequencedCollectionGets the first element.- Specified by:
getFirstin interfaceReadOnlySequencedCollection<E>- Returns:
- an element
-
getLast
Description copied from interface:ReadOnlySequencedCollectionGets the last element.- Specified by:
getLastin interfaceReadOnlySequencedCollection<E>- Returns:
- an element
-
addAll
Description copied from interface:ImmutableSetReturns a copy of this set that contains all elements of this set and also all elements of the specified collection.- Specified by:
addAllin interfaceImmutableCollection<E>- Specified by:
addAllin interfaceImmutableNavigableSet<E>- Specified by:
addAllin interfaceImmutableSet<E>- Specified by:
addAllin interfaceImmutableSortedSet<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
Description copied from interface:ReadOnlyNavigableSetReturns the least element in this set greater than or equal to the given element, or null if there is no such element.- Specified by:
ceilingin interfaceReadOnlyNavigableSet<E>- Parameters:
e- the given element- Returns:
- ceiling element or null
-
empty
Description copied from interface:ImmutableSetReturns an empty set instance that has the specified element type.- Specified by:
emptyin interfaceImmutableCollection<E>- Specified by:
emptyin interfaceImmutableNavigableSet<E>- Specified by:
emptyin interfaceImmutableSet<E>- Specified by:
emptyin interfaceImmutableSortedSet<E>- Type Parameters:
T- the element type of the returned set- Returns:
- an empty set of the specified element type.
-
empty
Description copied from interface:ImmutableSortedSetReturns a copy of this collection that is empty, and has the specified type and comparator.- Specified by:
emptyin interfaceImmutableSortedSet<E>- Type Parameters:
T- the element type of the collection- Parameters:
comparator- a comparator for ordering the elements of the set, specifynullto use the natural order of the elements- Returns:
- an empty collection of the specified type and comparator
-
comparator
Description copied from interface:ReadOnlySortedSetReturns the comparator used to order the elements in this set, ornullif this set uses the natural ordering of its elements.- Specified by:
comparatorin interfaceReadOnlySortedSet<E>- Returns:
- comparator or null
-
contains
Description copied from interface:ReadOnlyCollectionReturnstrueif this collection contains the specified object.- Specified by:
containsin interfaceReadOnlyCollection<E>- Parameters:
o- an object- Returns:
trueif this collection contains the specified object
-
floor
Description copied from interface:ReadOnlyNavigableSetReturns the greatest element in this set less than or equal to the given element, or null if there is no such element.- Specified by:
floorin interfaceReadOnlyNavigableSet<E>- Parameters:
e- the given element- Returns:
- floor element or null
-
higher
Description copied from interface:ReadOnlyNavigableSetReturns the least element in this set greater than the given element, or null if there is no such element.- Specified by:
higherin interfaceReadOnlyNavigableSet<E>- Parameters:
e- the given element- Returns:
- higher element or null
-
iterator
Description copied from interface:ReadOnlyCollectionReturns an iterator over the elements in this collection. -
spliterator
- Specified by:
spliteratorin interfaceIterable<E>
-
lower
Description copied from interface:ReadOnlyNavigableSetReturns the greatest element in this set less than the given element, or null if there is no such element.- Specified by:
lowerin interfaceReadOnlyNavigableSet<E>- Parameters:
e- the given element- Returns:
- lower element or null
-
maxSize
public int maxSize()Description copied from interface:ImmutableCollectionReturns the maximal number of elements that this collection type can hold- Specified by:
maxSizein interfaceImmutableCollection<E>- Returns:
- the maximal size
-
readOnlyReversed
Description copied from interface:ReadOnlySequencedSetReturns a reversed-order view of this set. Changes to the underlying set are visible in the reversed view.- Specified by:
readOnlyReversedin interfaceReadOnlySequencedCollection<E>- Specified by:
readOnlyReversedin interfaceReadOnlySequencedSet<E>- Returns:
- a reversed-order view of this set
-
remove
Description copied from interface:ImmutableSetReturns a copy of this set that contains all elements of this set except the specified element.- Specified by:
removein interfaceImmutableCollection<E>- Specified by:
removein interfaceImmutableNavigableSet<E>- Specified by:
removein interfaceImmutableSet<E>- Specified by:
removein interfaceImmutableSortedSet<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
Description copied from interface:ImmutableSetReturns a copy of this set that contains all elements of this set except the elements of the specified collection.- Specified by:
removeAllin interfaceImmutableCollection<E>- Specified by:
removeAllin interfaceImmutableNavigableSet<E>- Specified by:
removeAllin interfaceImmutableSet<E>- Specified by:
removeAllin interfaceImmutableSortedSet<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
Description copied from interface:ImmutableSetReturns a copy of this set that contains only elements that are in this set and in the specified collection.- Specified by:
retainAllin interfaceImmutableCollection<E>- Specified by:
retainAllin interfaceImmutableNavigableSet<E>- Specified by:
retainAllin interfaceImmutableSet<E>- Specified by:
retainAllin interfaceImmutableSortedSet<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:ReadOnlyCollectionReturns the size of the collection.- Specified by:
sizein interfaceReadOnlyCollection<E>- Returns:
- the size
-
hashCode
public int hashCode()Description copied from interface:ReadOnlySetReturns 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:
hashCodein interfaceReadOnlySet<E>- Overrides:
hashCodein classObject- Returns:
- the hash code value for this set
- See Also:
-
equals
Description copied from interface:ReadOnlySetCompares the specified object with this set for equality.Returns
trueif 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:
equalsin interfaceReadOnlySet<E>- Overrides:
equalsin classObject- Parameters:
other- an object- Returns:
trueif the object is equal to this map
-
toMutable
Description copied from interface:ImmutableSetReturns a mutable copy of this set.- Specified by:
toMutablein interfaceImmutableCollection<E>- Specified by:
toMutablein interfaceImmutableNavigableSet<E>- Specified by:
toMutablein interfaceImmutableSet<E>- Specified by:
toMutablein interfaceImmutableSortedSet<E>- Returns:
- a mutable copy.
-
toString
-