SplayTreeSet<E>.from constructor Null safety

SplayTreeSet<E>.from(
  1. Iterable elements,
  2. [int compare(
    1. E key1,
    2. E key2
    ),
  3. bool isValidKey(
    1. dynamic potentialKey
    )]
)

Creates a SplayTreeSet that contains all elements.

The set works as if created by new SplayTreeSet<E>(compare, isValidKey).

All the elements should be instances of E and valid arguments to compare. The elements iterable itself may have any element type, so this constructor can be used to down-cast a Set, for example as:

Set<SuperType> superSet = ...;
Set<SubType> subSet =
    SplayTreeSet<SubType>.from(superSet.whereType<SubType>());

Implementation

factory SplayTreeSet.from(Iterable elements,
    [int Function(E key1, E key2)? compare,
    bool Function(dynamic potentialKey)? isValidKey]) {
  if (elements is Iterable<E>) {
    return SplayTreeSet<E>.of(elements, compare, isValidKey);
  }
  SplayTreeSet<E> result = SplayTreeSet<E>(compare, isValidKey);
  for (var element in elements) {
    result.add(element as dynamic);
  }
  return result;
}