SplayTreeSet<E>.from 构造函数
创建一个包含所有 elements 的 SplayTreeSet。
该集合的工作方式就像通过 SplayTreeSet<E>(compare, isValidKey) 创建的一样。
所有 elements 应该是 E 的实例,并且是 compare 的有效参数。该 elements 可迭代本身可以有任何元素类型,因此此构造函数可用于向下转换 Set,例如:
Set<SuperType> superSet = ...;
Set<SubType> subSet =
SplayTreeSet<SubType>.from(superSet.whereType<SubType>());
示例
final numbers = <num>[20, 30, 10];
final setFrom = SplayTreeSet<int>.from(numbers);
print(setFrom); // {10, 20, 30}
实现
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;
}