difference 方法

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

创建一个新集合,包含本集合中不属于 other 的元素。

也就是说,返回的集合包含所有这个 Set 中的元素,但不包含 other 中的元素,根据 other.contains 的结果。

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

实现

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