intersection 方法

Set<E> intersection(
  1. Set<Object?> other
)
override

创建一个新集合,它是当前集合与other的交集。

也就是说,返回的集合包含所有当前Set中也是other中的元素,这是根据other.contains确定的。

final characters1 = <String>{'A', 'B', 'C'};
final characters2 = <String>{'A', 'E', 'F'};
final intersectionSet = characters1.intersection(characters2);
print(intersectionSet); // {A}

实现

Set<E> intersection(Set<Object?> other) {
  Set<E> result = SplayTreeSet<E>(_compare, _validKey);
  for (E element in this) {
    if (other.contains(element)) result.add(element);
  }
  return result;
}