Collections
Set Operations
Dart Set exposes the classic set-algebra methods on top of plain membership. a.intersection(b) is the set of values present in both, a.union(b) is everything in either, and a.difference(b) is everything in a that is not in b. Each call returns a fresh Set; a and b are not modified. The result preserves insertion order from a, with new values from b appearing after for union.
Program
Play the program to compute the intersection, union, and difference of two color sets and print all three.
set_operations.dart
void main() {
var a = {'red', 'green', 'blue'};
var b = {'green', 'blue', 'yellow'};
var both = a.intersection(b);
var either = a.union(b);
var onlyA = a.difference(b);
print('both=$both union=$either');
print('only=$onlyA');
}
intersection
`a.intersection(b)` is the set of values present in both `a` and `b`. Neither input is modified; a fresh `Set` is returned.
union
`a.union(b)` is the set of every value in `a` or `b`. Values from `a` come first in insertion order, with any new values from `b` appended.
difference
`a.difference(b)` is the set of values in `a` that are not in `b`. Use it to drop a set of unwanted entries from another set.