SplayTreeSet<E> classfinal
ASet of objects that can be ordered relative to each other.
The set is based on a self-balancing binary tree. It allows most operationsin amortized logarithmic time.
Elements of the set are compared using thecompare function passed inthe constructor, both for ordering and for equality.If the set contains only an objecta, thenset.contains(b)will returntrue if and only ifcompare(a, b) == 0,and the value ofa == b is not even checked.If the compare function is omitted, the objects are assumed to beComparable, and are compared using theirComparable.compareTo method.Non-comparable objects (includingnull) will not work as an elementin that case.
Note:Do not modify a set (add or remove elements) while an operationis being performed on that set, for example in functionscalled during aforEach orcontainsAll call,or while iterating the set.
Do not modify elements in a way which changes their equality (and thus theirhash code) while they are in the set. Some specialized kinds of sets may bemore permissive with regards to equality, in which case they should documenttheir different behavior and restrictions.
Example:
final planets = SplayTreeSet<String>((a, b) => a.compareTo(b));To add data to a set, useadd oraddAll.
planets.add('Neptune');planets.addAll({'Venus', 'Mars', 'Earth', 'Jupiter'});print(planets); // {Earth, Jupiter, Mars, Neptune, Venus}To check if the set is empty, useisEmpty orisNotEmpty.To find the number of elements in the set, uselength.
final isEmpty = planets.isEmpty; // falsefinal length = planets.length; // 5To check whether the set contains a specific element, usecontains.
final marsExists = planets.contains('Mars'); // trueTo get element value using index, useelementAt.
final elementAt = planets.elementAt(1);print(elementAt); // JupiterTo make a copy of set, usetoSet.
final copySet = planets.toSet(); // a `SplayTreeSet` with the same ordering.print(copySet); // {Earth, Jupiter, Mars, Neptune, Venus}To remove an element, useremove.
final removedValue = planets.remove('Mars'); // trueprint(planets); // {Earth, Jupiter, Neptune, Venus}To remove multiple elements at the same time, useremoveWhere.
planets.removeWhere((element) => element.startsWith('J'));print(planets); // {Earth, Neptune, Venus}To removes all elements in this set that do not meet a condition,useretainWhere.
planets.retainWhere((element) => element.contains('Earth'));print(planets); // {Earth}To remove all elements and empty the set, useclear.
planets.clear();print(planets.isEmpty); // trueprint(planets); // {}See also:
- Set is a base-class for collection of objects.
- HashSet the order of the objects in the iterations is not guaranteed.
- LinkedHashSet objects stored based on insertion order.
- Mixed-in types
- Available extensions
Constructors
- SplayTreeSet([intcompare(Ekey1,Ekey2)?,boolisValidKey(dynamicpotentialKey)?])
- Create a newSplayTreeSet with the given compare function.
- SplayTreeSet.from(Iterableelements, [intcompare(Ekey1,Ekey2)?,boolisValidKey(dynamicpotentialKey)?])
- Creates aSplayTreeSet that contains all
elements.factory - SplayTreeSet.of(Iterable<
E> elements, [intcompare(Ekey1,Ekey2)?,boolisValidKey(dynamicpotentialKey)?]) - Creates aSplayTreeSet from
elements.factory
Properties
- first→ E
- The first element.no setteroverride
- firstOrNull→ T?
Available onIterable<
The first element of this iterator, orT> , provided by theIterableExtensions extensionnullif the iterable is empty.no setter- hashCode→int
- The hash code for this object.no setterinherited
- indexed→Iterable<
(int,T)> Available onIterable<
Pairs of elements of the indices and elements of this iterable.T> , provided by theIterableExtensions extensionno setter- isEmpty→bool
- Whether this collection has no elements.no setteroverride
- isNotEmpty→bool
- Whether this collection has at least one element.no setteroverride
- iterator→Iterator<
E> - A new
Iteratorthat allows iterating the elements of thisIterable.no setteroverride - last→ E
- The last element.no setteroverride
- lastOrNull→ T?
Available onIterable<
The last element of this iterable, orT> , provided by theIterableExtensions extensionnullif the iterable is empty.no setter- length→int
- The number of elements in thisIterable.no setteroverride
- nonNulls→Iterable<
T> Available onIterable<
The non-T?> , provided by theNullableIterableExtensions extensionnullelements of this iterable.no setter- runtimeType→Type
- A representation of the runtime type of the object.no setterinherited
- single→ E
- Checks that this iterable has only one element, and returns that element.no setteroverride
- singleOrNull→ T?
Available onIterable<
The single element of this iterator, orT> , provided by theIterableExtensions extensionnull.no setter- wait→Future<
List< T> > Available onIterable<
Waits for futures in parallel.Future< , provided by theFutureIterable extensionT> >no setter
Methods
- add(
Eelement)→bool - Adds
valueto the set.override - addAll(
Iterable< E> elements)→ void - Adds all
elementsto this set.override - any(
booltest(Eelement))→bool - Checks whether any element of this iterable satisfies
test.inherited - asNameMap(
)→Map< String,T> Available onIterable<
Creates a map from the names of enum values to the values.T> , provided by theEnumByName extension- byName(
Stringname)→ T Available onIterable<
Finds the enum value in this list with nameT> , provided by theEnumByName extensionname.- cast<
R> ()→Set< R> - A view of this iterable as an iterable of
Rinstances.override - clear(
)→ void - Removes all elements from the set.override
- contains(
Object?element)→bool - Whether the collection contains an element equal to
element.override - containsAll(
Iterable< Object?> other)→bool - Whether this set contains all the elements of
other.inherited - difference(
Set< Object?> other)→Set<E> - Creates a new set with the elements of this that are not in
other.override - elementAt(
intindex)→ E - Returns the
indexth element.inherited - elementAtOrNull(
intindex)→ T? Available onIterable<
The element at positionT> , provided by theIterableExtensions extensionindexof this iterable, ornull.- every(
boolf(Eelement))→bool - Checks whether every element of this iterable satisfies
test.inherited - expand<
T> (Iterable< T> f(Eelement))→Iterable<T> - Expands each element of thisIterable into zero or more elements.inherited
- firstWhere(
booltest(Evalue), {EorElse()?})→ E - The first element that satisfies the given predicate
test.inherited - fold<
T> (TinitialValue,Tcombine(TpreviousValue,Eelement))→ T - Reduces a collection to a single value by iteratively combining eachelement of the collection with an existing valueinherited
- followedBy(
Iterable< E> other)→Iterable<E> - Creates the lazy concatenation of this iterable and
other.inherited - forEach(
voidf(Eelement))→ void - Invokes
actionon each element of this iterable in iteration order.inherited - intersection(
Set< Object?> other)→Set<E> - Creates a new set which is the intersection between this set and
other.override - join(
[Stringseparator =""])→String - Converts each element to aString and concatenates the strings.inherited
- lastWhere(
booltest(Evalue), {EorElse()?})→ E - The last element that satisfies the given predicate
test.inherited - lookup(
Object?object)→ E? - If an object equal to
objectis in the set, return it.override - map<
T> (Tf(Eelement))→Iterable< T> - The current elements of this iterable modified by
toElement.inherited - noSuchMethod(
Invocationinvocation)→ dynamic - Invoked when a nonexistent method or property is accessed.inherited
- reduce(
Ecombine(Evalue,Eelement))→ E - Reduces a collection to a single value by iteratively combining elementsof the collection using the provided function.inherited
- remove(
Object?object)→bool - Removes
valuefrom the set.override - removeAll(
Iterable< Object?> elements)→ void - Removes each element of
elementsfrom this set.override - removeWhere(
booltest(Eelement))→ void - Removes all elements of this set that satisfy
test.inherited - retainAll(
Iterable< Object?> elements)→ void - Removes all elements of this set that are not elements in
elements.override - retainWhere(
booltest(Eelement))→ void - Removes all elements of this set that fail to satisfy
test.inherited - singleWhere(
booltest(Evalue), {EorElse()?})→ E - The single element that satisfies
test.inherited - skip(
intn)→Iterable< E> - Creates anIterable that provides all but the first
countelements.inherited - skipWhile(
booltest(Evalue))→Iterable< E> - Creates an
Iterablethat skips leading elements whiletestis satisfied.inherited - take(
intn)→Iterable< E> - Creates a lazy iterable of the
countfirst elements of this iterable.inherited - takeWhile(
booltest(Evalue))→Iterable< E> - Creates a lazy iterable of the leading elements satisfying
test.inherited - toList(
{boolgrowable =true})→List< E> - Creates aList containing the elements of thisIterable.inherited
- toSet(
)→Set< E> - Creates aSet containing the same elements as this iterable.override
- toString(
)→String - A string representation of this object.override
- union(
Set< E> other)→Set<E> - Creates a new set which contains all the elements of this set and
other.override - where(
boolf(Eelement))→Iterable< E> - Creates a new lazyIterable with all elements that satisfy thepredicate
test.inherited - whereType<
T> ()→Iterable< T> - Creates a new lazyIterable with all elements that have type
T.inherited
Operators
- operator ==(
Objectother)→bool - The equality operator.inherited