ELI5: Union and Intersection of Sets
elementary set theory - What are the union and intersection class? - Mathematics Stack Exchange
collections - Union or intersection of Java Sets - Stack Overflow
[University] Set Theory - Union and Intersection
What is the difference between a union and an intersection of sets?
What Is the Difference Between Set Intersection and Set Union?
How do you find the union and intersection of sets?
Videos
I need help figuring out how to find the Union or intersection of sets when provided with inequalities.
For example: Find the intersection of sets {xlx<9},{xlx<-8} Find the Union of sets {xlx<9}, {xlx<-8}
Thank you!
When we speak of $\bigcup A$ and $\bigcap A$, we require all the elements of $A$ to themselves be sets.
Depending on the precise foundational theory you're working in, one of a few things can happen.
- You're working in some variation of ZF set theory without atoms/urelements - that is you're working in a 1-sorted set theory - which does not support classes.
A 1-sorted theory is, as the name suggests, a logical theory in which there's only 1 sort of thing. In ZF, the only sort of thing that exists is sets. All the "things" discussed in ZF are sets.
In this case, all elements of $A$ are automatically sets, so the notation always makes sense.
In ZF, one typically defines $0 = \{\}$, $1 = \{0\}$, $2 = \{0, 1\}$, $3 = \{0, 1, 2\}$. In this case, we see that $\bigcup \{1, 2, 3\} = \{0, 1, 2\} = 3$. In fact, if $A$ is a finite set of natural numbers, $\bigcup A$ will be the largest element of $A$ and $\bigcap A$ will be the smallest element.
However, there is a small wrinkle with ZF-like theories. The wrinkle is that $\bigcap A$ is not defined when $A = \emptyset$. This is because according to the definition of $\bigcap \emptyset$, we would expect absolutely anything to be an element of $\bigcap \emptyset$ - that is, $\bigcap \emptyset$ would be the set of all sets. But we know there is no set of all sets, since if there were such a thing we would have Russell's Paradox.
- You're working in some variation of NBG set theory without atoms/urelements.
In NBG set theory, the things one quantifies over are "classes". There is, for example, a class of all sets. A "set" is a special kind of class which is "small enough" in some sense.
This allows a different approach to axioms than ZF. In ZF, there is an axiom saying that for all $a$, there is a set $P(a)$ of all subsets of $a$. In NBG, given a set $a$, you can immediately define the class $P(a) = \{b \mid b \subseteq a\}$; the axiom says that $P(a)$ is a set.
In NBG set theory, the intersection $\bigcap A$ is always defined. But if $A$ is empty, the intersection will not be a set. It will instead be a "proper class" - that is, a class which is not a set.
- You work in some variation of NBG or ZF with atoms/urelements.
In this version of events, the "things" one quantifies over might be "atoms". An atom is something which is not a set/class and which is thus not defined solely in terms of its elements (atoms are generally taken to have no elements at all). In such a set theory, we would only discuss $\bigcap A$ and $\bigcup A$ when all elements of $A$ are sets (not atoms), though (assuming we adopt the convention that atoms have no elements) the notation $\bigcap A$ and $\bigcup A$ makes sense even when $A$ has atoms - in this case, $\bigcup A = \bigcup \{x \in A \mid x$ a set$\}$ and $\bigcup A = \emptyset$.
- You work in some version of type theory (including, for the purposes of this discussion, topos theory).
In type theory, everything has a "type". It does not make sense in type theory to take the union or intersection of types.
However, given a type $T$, one can (sometimes) form the type $P(T)$, which is (roughly) the type of all subsets of $A$. Given some $A : P(P(T))$, one can define $\bigcup A = \{x : T \mid \exists y \in A (x \in y)\}$ and $\bigcap A = \{x : T \mid \forall y \in A (x \in y)\} : P(T)$.
Here, $t : T$ means "$t$ has type $T$".
In this account, $\bigcap A$ is actually well-defined even when $A = \emptyset$.
In type theory, trying to take the union $\bigcup \{1, 2, 3\}$ would be a "type error". One cannot even discuss it. This is because $1, 2, 3$ are not sets.
The type theoretic/category theoretic account of intersections and unions is a bit more subtle when one does not have "power set" types. I will not go into that here.
Let me bring general formal definition of union with respect to indexed family:
Suppose we have set $X$, called indices set, and for each $\alpha \in X$ is defined some $U_\alpha$ set. Then, by definition, set
$$\bigcup\limits_{\alpha \in X}U_\alpha=\{x\colon \exists \alpha \in X, x\in U_\alpha\}$$
is called union of indexed family $\{U_\alpha\}_{\alpha \in X}$ with respect to indices set $X$. If, for example, we take $X=\{1,2, 3\}$, then we obtain union of tree sets asked. For $X=\{1, \cdots, n\}$ it is union of $n$ sets etc.
For intersection existential quantifier is changed to universal.
Very interesting is example where $X= \emptyset$.
The simplest one-line solution is this:
set1.addAll(set2); // Union
set1.retainAll(set2); // Intersection
The above solution is destructive, meaning that contents of the original set1 my change.
If you don't want to touch your existing sets, create a new set:
var result = new HashSet<>(set1); // In Java 10 and above
Set<Integer> result = new HashSet<>(set1); // In Java < 10
result.addAll(set2); // Union
result.retainAll(set2); // Intersection
While guava for sure is neater and pretty much standard, here's a non destructive way to do union and intersect using only standard Java
Set s1 = Set.of(1,2,3);
Set s2 = Set.of(3,4,5);
Set union = Stream.concat(s1.stream(),s2.stream()).collect(Collectors.toSet());
Set intersect = s1.stream().filter(s2::contains).collect(Collectors.toSet());
https://imgur.com/t9By6Rr
https://imgur.com/GJl3SAT
Having trouble understanding my lecture notes and the specific notation they use. For context, this is the first time the union and intersection symbols appear. I'm pretty sure they big U and big upside down U mean 'union' and 'intersection' respectively. But I don't understand (We define U A (sub lambda) to be the set whose elements that belong to at least one of the A (sub lambda)). I find the wording to be incredibly confusing, and if anyone could please explain it in simpler terms. Any help is greatly appreciated.