BTreeSet<E> class

An ordered, in-memory set backed by an order-statistic B+Tree.

Elements are kept sorted according to a Comparator. All elements reside in doubly-linked leaf nodes, while internal nodes store routing keys and subtree element counts.

Advantages over SplayTreeSet, SortedList, and List

  • vs. SplayTreeSet: Delivers O(log n) positional rank/select (elementAt, indexOf, removeAt) instead of O(n) scans, and O(1) step doubly-linked leaf iteration for faster full iteration and range scans. Read queries do not mutate tree structure (no splaying), while single-element lookups remain comparable and mutations trade slightly higher constant-factor array shifts for shallower O(log_B n) tree height and fewer heap allocations.
  • vs. SortedList: Supports O(log n) insertions and removals instead of O(n) contiguous list shifts, making bulk construction and mutations much faster while retaining O(log n) positional access and binary search (SortedList remains faster for static O(1) indexing and iteration).
  • vs. List: Maintains elements in sorted order automatically on insertion with O(log n) membership checks, range queries, and neighbor searches (floor, ceiling), whereas an unsorted List requires O(n) linear scan or O(n log n) full re-sorting.

Sizing minEntries and maxEntries

  • maxEntries (M) defines the maximum number of elements in a leaf node and children in an internal node before splitting (M >= 4).
  • minEntries (m) defines the underflow threshold (2 <= m <= M ~/ 2).
  • Default values (minEntries = 16, maxEntries = 32) balance CPU cache locality and intra-node array shifting for general in-memory workloads.
final set = BTreeSet<int>.of([30, 10, 20, 40]);
print(set.elementAt(2)); // 30
print(set.indexOf(20)); // 1
print(set.ceiling(25)); // 30
print(set.range(min: 15, max: 35).toList()); // [20, 30]
Inheritance
Available extensions

Constructors

BTreeSet({Comparator<E>? comparator, int? minEntries, int? maxEntries})
Constructs an empty BTreeSet with an optional comparator, minEntries, and maxEntries.
BTreeSet.from(Iterable<Object?> elements, {Comparator<E>? comparator, int? minEntries, int? maxEntries})
Constructs a BTreeSet containing all elements of elements, casting each element to E.
factory
BTreeSet.of(Iterable<E> elements, {Comparator<E>? comparator, int? minEntries, int? maxEntries})
Constructs a BTreeSet containing all elements of elements.
factory

Properties

comparator → Comparator<E>
Comparator used to order elements in this set.
no setter
first → E
The first element.
no setteroverride
firstOrNull → T?

Available on Iterable<T>, provided by the IterableExtensions extension

The first element of this iterator, or null if the iterable is empty.
no setter
hashCode → int
The hash code for this object.
no setterinherited
indexed → Iterable<(int, T)>

Available on Iterable<T>, provided by the IterableExtensions extension

Pairs of elements of the indices and elements of this iterable.
no 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>
An iterator that iterates over the elements of this set.
no setteroverride
last → E
The last element.
no setteroverride
lastOrNull → T?

Available on Iterable<T>, provided by the IterableExtensions extension

The last element of this iterable, or null if the iterable is empty.
no setter
length → int
The number of elements in this Iterable.
no setteroverride
maxEntries → int
Maximum number of entries per leaf or children per internal node.
no setter
minEntries → int
Minimum number of entries per non-root node.
no setter
nonNulls → Iterable<T>

Available on Iterable<T?>, provided by the NullableIterableExtensions extension

The non-null elements of this iterable.
no setter
reversed → Iterable<E>
An iterable of all elements in descending order.
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 setterinherited
singleOrNull → T?

Available on Iterable<T>, provided by the IterableExtensions extension

The single element of this iterator, or null.
no setter
toJSIterable → JSIterable<T>

Available on Iterable<T>, provided by the IterableToJSIterable extension

A JSIterable wrapper that proxies to the Dart iterable API.
no setter
wait → Future<List<T>>

Available on Iterable<Future<T>>, provided by the FutureIterable extension

Waits for futures concurrently.
no setter

Methods

add(E value) → bool
Adds value to the set.
override
addAll(Iterable<E> elements) → void
Adds all elements to this set.
inherited
any(bool test(E value)) → bool
Checks whether any element of this iterable satisfies test.
inherited
asNameMap() → Map<String, T>

Available on Iterable<T>, provided by the EnumByName extension

Creates a map from the names of enum values to the values.
atRandom({Random? random, E orElse()?}) → E

Available on Iterable<E>, provided by the RandomIterableExtension extension

Returns a random element from this Iterable.
byName(String name) → T

Available on Iterable<T>, provided by the EnumByName extension

Finds the enum value in this list with name name.
cast<R>() → Set<R>
Provides a view of this set as a set of R instances.
inherited
ceiling(E element) → E?
Returns the least element greater than or equal to element in O(log n) time, or null if no such element exists.
chunked(int size) → Iterable<List<E>>

Available on Iterable<E>, provided by the ChunkedIterableExtension extension

Divides this Iterable into sub-lists of a given size. The final list might be smaller or equal to the desired size.
chunkedWithPadding(int size, E padding) → Iterable<List<E>>

Available on Iterable<E>, provided by the ChunkedIterableExtension extension

Divides this Iterable into sub-lists of a given size. The final list is expanded with the provided padding.
clear() → void
Removes all elements from the set.
override
closeMatches(String target, {int count = 3, double cutoff = 0.6}) → Iterable<String>

Available on Iterable<String>, provided by the CloseMatchesOnStringIterable extension

Returns a list of the best "good enough" matches of a list of strings.
closeMatches(Iterable<T> target, {int count = 3, double cutoff = 0.6}) → Iterable<Iterable<T>>

Available on Iterable<Iterable<T>>, provided by the CloseMatchesOnIterable extension

Returns a list of the best "good enough" matches.
combinations(int count, {bool repetitions = false}) → Iterable<List<E>>

Available on Iterable<E>, provided by the CombinationsIterableExtension extension

Returns an iterable over the combinations of this Iterable of length count. The combinations are emitted in lexicographical order based on the input.
contains(Object? element) → bool
Whether value is in the set.
override
containsAll(Iterable<Object?> other) → bool
Whether this set contains all the elements of other.
inherited
count(bool predicate(E element)) → int

Available on Iterable<E>, provided by the CountIterableExtension extension

Returns the number of times predicate evaluates to true.
deepFlatten<E>() → Iterable<E>

Available on Iterable, provided by the DeepFlattenIterableExtension extension

Flattens arbitrarily nested Iterables with elements of type E. Throws an ArgumentError when encountering a value of an unexpected type.
difference(Set<Object?> other) → Set<E>
Creates a new set with the elements of this that are not in other.
inherited
elementAt(int index) → E
Returns the element at the sorted index in O(log n) time.
override
elementAtOrNull(int index) → T?

Available on Iterable<T>, provided by the IterableExtensions extension

The element at position index of this iterable, or null.
every(bool f(E value)) → bool
Checks whether every element of this iterable satisfies test.
inherited
expand<T>(Iterable<T> f(E element)) → Iterable<T>
Expands each element of this Iterable into zero or more elements.
inherited
firstWhere(bool test(E value), {E orElse()?}) → E
The first element that satisfies the given predicate test.
inherited
flatMap<T>(Iterable<T> callback(E element)) → Iterable<T>

Available on Iterable<E>, provided by the FlatMapIterableExtension extension

Maps each element of this Iterable using a mapping function to zero or more elements, then flattens the result into a continuous iterable.
flatten() → Iterable<E>

Available on Iterable<Iterable<E>>, provided by the FlattenIterableExtension extension

Flattens an Iterable of Iterables to a flattened Iterable.
floor(E element) → E?
Returns the greatest element less than or equal to element in O(log n) time, or null if no such element exists.
fold<T>(T initialValue, T combine(T previousValue, E element)) → T
Reduces a collection to a single value by iteratively combining each element of the collection with an existing value
inherited
followedBy(Iterable<E> other) → Iterable<E>
Creates the lazy concatenation of this iterable and other.
inherited
forEach(void f(E element)) → void
Invokes action on each element of this iterable in iteration order.
inherited
gcd() → BigInt

Available on Iterable<BigInt>, provided by the GcdBigIntIterableExtension extension

Returns greatest common divisor (GCD) of values in this Iterable. This is the largest positive integer that divides all numbers.
gcd() → int

Available on Iterable<int>, provided by the GcdIntegerIterableExtension extension

Returns greatest common divisor (GCD) of values in this Iterable. This is the largest positive integer that divides all numbers.
groupBy<K>([K key(V element)?]) → Iterable<Group<K, V>>

Available on Iterable<V>, provided by the GroupIterableExtension extension

Groups consecutive keys of this Iterable.
higher(E element) → E?
Returns the least element strictly greater than element in O(log n) time, or null if no such element exists.
indexed({int start = 0, int step = 1, int? offset}) → Iterable<Indexed<E>>

Available on Iterable<E>, provided by the IndexedIterableExtension extension

Returns an iterable that combines the index and value of this Iterable.
indexOf(Object? element) → int
Returns the sorted 0-based rank of element in O(log n) time, or -1 if not present.
indices({int step = 1}) → Range<int>

Available on Iterable<Object?>, provided by the IndicesIterableExtension extension

Returns a Range of the indices of this iterable that can be accessed with the [] operator.
intersection(Set<Object?> other) → Set<E>
Creates a new set which is the intersection between this set and other.
inherited
join([String separator = ""]) → String
Converts each element to a String and concatenates the strings.
inherited
largest(int count, {Comparator<E>? comparator}) → List<E>

Available on Iterable<E>, provided by the OperatorsIterableExtension extension

Returns a list of the count largest elements of this Iterable. The elements need to be Comparable, unless a custom comparator is provided.
lastWhere(bool test(E value), {E orElse()?}) → E
The last element that satisfies the given predicate test.
inherited
lcm() → int

Available on Iterable<int>, provided by the LcmIntegerIterableExtension extension

Returns the least common multiple (LCM) of the values in this Iterable. This is the smallest positive integer that is divisible by all numbers.
lcm() → BigInt

Available on Iterable<BigInt>, provided by the LcmBigIntIterableExtension extension

Returns the least common multiple (LCM) of the values in this Iterable. This is the smallest positive integer that is divisible by all numbers.
lookup(Object? element) → E?
If an object equal to object is in the set, return it.
override
lower(E element) → E?
Returns the greatest element strictly less than element in O(log n) time, or null if no such element exists.
map<T>(T f(E element)) → Iterable<T>
The current elements of this iterable modified by toElement.
inherited
max({Comparator<E>? comparator, E orElse()?}) → E

Available on Iterable<E>, provided by the OperatorsIterableExtension extension

Returns the maximum of this Iterable. The elements need to be Comparable, unless a custom comparator is provided.
min({Comparator<E>? comparator, E orElse()?}) → E

Available on Iterable<E>, provided by the OperatorsIterableExtension extension

Returns the minimum of this Iterable. The elements need to be Comparable, unless a custom comparator is provided.
minMax({Comparator<E>? comparator, ({E max, E min}) orElse()?}) → ({E max, E min})

Available on Iterable<E>, provided by the OperatorsIterableExtension extension

Returns the minimum and maximum of this Iterable at once. The elements need to be Comparable, unless a custom comparator is provided.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
occurrences(E element) → int

Available on Iterable<E>, provided by the CountIterableExtension extension

Returns the number of times element appears in the iterable.
pairwise() → Iterable<(E, E)>

Available on Iterable<E>, provided by the PairwiseIterableExtension extension

An iterable over the successive overlapping pairs of this iterable.
partition(bool test(E element)) → ({List<E> falsey, List<E> truthy})

Available on Iterable<E>, provided by the PartitionIterableExtension extension

Splits this iterable into two lists: the truthy list where the test predicate is true and the falsey list where the test predicate is false.
permutations([int? count]) → Iterable<List<E>>

Available on Iterable<E>, provided by the PermutationIterableExtension extension

Returns an iterable over the permutations of this Iterable of length count. If no count is specified all full-length permutations are generated. The permutations are emitted in lexicographical order based on the input.
polynomial([num x = 10]) → num

Available on Iterable<num>, provided by the PolynomialIterableExtension extension

Evaluates the polynomial described by this Iterables coefficients and the value x.
powerSet() → Iterable<List<E>>

Available on Iterable<E>, provided by the PowerSetIterableExtension extension

Returns all subsets of this iterable including the empty set and the complete set itself. The power-set has 2^n elements, if the iterable has length n.
product({int repeat = 1}) → Iterable<List<E>>

Available on Iterable<Iterable<E>>, provided by the ProductIterableExtension extension

Returns an iterable over the cross product of this Iterable.
range({E? min, E? max, bool includeMin = true, bool includeMax = true, bool reversed = false}) → Iterable<E>
Returns a lazy iterable of elements between min and max in O(log n + k) time.
reduce(E combine(E value, E element)) → E
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
inherited
remove(Object? value) → bool
Removes value from the set.
override
removeAll(Iterable<Object?> elements) → void
Removes each element of elements from this set.
inherited
removeAt(int index) → E
Removes and returns the element at the sorted index in O(log n) time.
removeFirst() → E
Removes and returns the smallest element in O(log n) time.
removeLast() → E
Removes and returns the largest element in O(log n) time.
removeWhere(bool test(E value)) → void
Removes all elements of this set that satisfy test.
inherited
repeat({int? count}) → Iterable<E>

Available on Iterable<E>, provided by the RepeatIterableExtension extension

Returns an infinite iterable with the elements of this iterable. If count is provided the resulting iterator is limited to count repetitions.
retainAll(Iterable<Object?> elements) → void
Removes all elements of this set that are not elements in elements.
inherited
retainWhere(bool test(E value)) → void
Removes all elements of this set that fail to satisfy test.
inherited
separatedBy(Builder<E> separator, {Builder<E>? before, Builder<E>? after}) → Iterable<E>

Available on Iterable<E>, provided by the SeparatedIterableExtension extension

Returns an Iterable where every element is separated by an element built by a separator builder. Optionally specified before and after builders can provide an element at the beginning or the end of the non-empty iterable.
singleWhere(bool test(E value), {E orElse()?}) → E
The single element that satisfies test.
inherited
skip(int n) → Iterable<E>
Creates an Iterable that provides all but the first count elements.
inherited
skipWhile(bool test(E value)) → Iterable<E>
Creates an Iterable that skips leading elements while test is satisfied.
inherited
smallest(int count, {Comparator<E>? comparator}) → List<E>

Available on Iterable<E>, provided by the OperatorsIterableExtension extension

Returns a list of the count smallest elements of this Iterable. The elements need to be Comparable, unless a custom comparator is provided.
take(int n) → Iterable<E>
Creates a lazy iterable of the count first elements of this iterable.
inherited
takeWhile(bool test(E value)) → Iterable<E>
Creates a lazy iterable of the leading elements satisfying test.
inherited
toBiMap<K, V>({K key(E element)?, V value(E element)?}) → BiMap<K, V>

Available on Iterable<E>, provided by the BiMapOnIterableExtension extension

Converts this Iterable to a BiMap.
toBitList({bool growable = false}) → BitList

Available on Iterable<bool>, provided by the BitListExtension extension

Converts this Iterable to a space-efficient BitList.
toBTreeSet({Comparator<E>? comparator, int? minEntries, int? maxEntries}) → BTreeSet<E>

Available on Iterable<E>, provided by the BTreeSetExtension extension

Converts this iterable to a BTreeSet.
toComparator() → Comparator<T>

Available on Iterable<Comparator<T>>, provided by the CompoundIterableComparator extension

Returns a Comparator that tries each of the comparators in this iterable in order and returns the first result that doesn't end up in a tie.
toList({bool growable = true}) → List<E>
Creates a List containing the elements of this Iterable.
inherited
toListMultimap<K, V>({K key(E element)?, V value(E element)?, Map<K, List<V>>? map, Factory<List<V>>? factory}) → ListMultimap<K, V>

Available on Iterable<E>, provided by the ListMultimapOnIterableExtension extension

Converts this Iterable to a ListMultimap.
toMap<K, V>({K key(E element)?, V value(E element)?}) → Map<K, V>

Available on Iterable<E>, provided by the ToMapIterableExtension extension

Returns a Map from an Iterable.
toMultiset() → Multiset<T>

Available on Iterable<T>, provided by the MultisetExtension extension

Converts this Iterable to a Multiset.
toOrderedList({bool growable = true}) → OrderedList<E>

Available on Iterable<E>, provided by the OrderedListIterableExtension extension

Converts this Iterable to an OrderedList.
toPrinter() → Printer<T>

Available on Iterable<Printer<T>>, provided by the SequencePrinterIterableExtension extension

Constructs a sequence of printers.
toSet() → Set<E>
Creates a Set with the same elements and behavior as this Set.
override
toSetMultimap<K, V>({K key(E element)?, V value(E element)?, Map<K, Set<V>>? map, Factory<Set<V>>? factory}) → SetMultimap<K, V>

Available on Iterable<E>, provided by the SetMultimapOnIterableExtension extension

Converts this Iterable to a SetMultimap.
toSortedList({Comparator<E>? comparator, bool growable = true}) → SortedList<E>

Available on Iterable<E>, provided by the SortedListIterableExtension extension

Converts this Iterable to a SortedList.
toString() → String
A string representation of this object.
inherited
union(Set<E> other) → Set<E>
Creates a new set which contains all the elements of this set and other.
inherited
unique({Set<E> factory()?, bool equals(E e1, E e2)?, int hashCode(E e)?}) → Iterable<E>

Available on Iterable<E>, provided by the UniqueIterableExtension extension

Returns a lazy iterable that filters out duplicates from this Iterator.
where(bool f(E value)) → Iterable<E>
Creates a new lazy Iterable with all elements that satisfy the predicate test.
inherited
whereType<T>() → Iterable<T>
Creates a new lazy Iterable with all elements that have type T.
inherited
window(int size, {int step = 1, bool includePartial = false}) → Iterable<List<E>>

Available on Iterable<E>, provided by the WindowIterableExtension extension

Sliding window of given size over this Iterable.
zip() → Iterable<List<E>>

Available on Iterable<Iterable<E>>, provided by the ZipIterableExtension extension

Combines the first, second, third, ... elements of each Iterable into a new list. The resulting iterable has the length of the shortest input iterable.
zipPartial() → Iterable<List<E?>>

Available on Iterable<Iterable<E>>, provided by the ZipIterableExtension extension

Combines the first, second, third, ... elements of each Iterable into a new list. The resulting iterable has the length of the longest input iterable, missing values are padded with null.
zipPartialWith(E padding) → Iterable<List<E>>

Available on Iterable<Iterable<E>>, provided by the ZipIterableExtension extension

Combines the first, second, third, ... elements of each Iterable into a new list. The resulting iterable has the length of the longest input iterable, missing values are padded with padding.

Operators

operator ==(Object other) → bool
The equality operator.
inherited