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;
}