Set Theory — Venn Diagrams
Computational Mathematics
Important Notice
Contents
What are Sets?
A set is a collection of distinct objects — each element appears only once. Order doesn't matter: {1, 2, 3} and {3, 1, 2} are the same set. In programming, sets work like Set data structures, where duplicates are automatically removed.
Sets are written with curly braces. We can list elements directly — A = {1, 2, 3} — or describe them with a rule: A = {x | x is even}, read as "A is the set of all x such that x is even". The Universal Set (U) contains all elements under consideration in a given context: everything that "exists" in our problem.
Venn Diagrams are the classic visual way to represent sets: each set is a circle, and the surrounding rectangle is the universe (U). When two circles overlap, the overlapping region represents elements that belong to both sets at the same time. The following sections show each operation with colored diagrams and practical software examples.
Example 1 — Sets Defined by List
ALL elements from A and B combined (no duplicates).
Merging two lists of user IDs from different databases — you want ALL unique users.
ONLY elements that are in BOTH A and B at the same time.
Finding common friends between two social media profiles.
Example 1 — Sets Defined by List
A \ B • B \ A
Elements in A that are NOT in B.
Users who have an account but never made a purchase.
Elements in B that are NOT in A.
Features requested by clients that are not yet in the product backlog.
Example 1 — Sets Defined by List
A' • B'
Everything in the universe that is NOT in A (= elements only in B, not shared).
All inactive users (complement of the active users set).
Everything in the universe that is NOT in B (= elements only in A, not shared).
Products not in the discount category.
Example 1 — Sets Defined by List
(A ∪ B)'
Everything NOT in A and NOT in B (here: empty — all elements belong to at least one set).
Users matching NO filter criteria — completely unclassified records.
Example 2 — Sets Defined by Rule
All even numbers OR numbers ≥ 10 (or both).
Numbers that are even AND ≥ 10.
Example 2 — Sets Defined by Rule
B' • (A ∪ B)'
All integers from 1 to 20 that are NOT ≥ 10 (i.e., numbers < 10).
Numbers that are NOT even AND NOT ≥ 10 (odd numbers less than 10).
Example 2 — Sets Defined by Rule
(A \ B)'
Everything EXCEPT elements only in A (keeps intersection, B-only, and outside).
Java — HashSet: Sets in Practice
How sets work in real code
import java.util.HashSet; import java.util.Set; public class Sets { public static void main(String[] args) { // Creates an empty set of integers Set<Integer> numSet = new HashSet<>(); System.out.println(numSet.size()); // → 0 (empty) numSet.add(5); // Adds 5 System.out.println(numSet.size()); // → 1 numSet.add(6); // Adds 6 System.out.println(numSet.size()); // → 2 numSet.add(5); // Tries to add 5 again System.out.println(numSet.size()); // → 2 (UNCHANGED! Set ignores duplicates) System.out.println(numSet.add(4)); // → true (4 was new, added) System.out.println(numSet.size()); // → 3 System.out.println(numSet.add(4)); // → false (4 already existed, rejected) System.out.println(numSet.size()); // → 3 (still 3) // Prints the entire set — order may vary! System.out.println(numSet); // → [4, 5, 6] } }
Java — Custom Objects in Sets
Why equals() and hashCode() matter
// Simple class with two attributes public class CoolClass { int value; // a number String text; // a text public CoolClass(int value, String text) { this.value = value; this.text = text; } }
// Creating objects with the SAME values CoolClass object1 = new CoolClass(1, "One"); CoolClass object2 = new CoolClass(2, "Two"); CoolClass objectOther1 = new CoolClass(1, "One"); // same value as object1! Set<CoolClass> coolSet = new HashSet<>(); System.out.println(coolSet.size()); // → 0 coolSet.add(object1); coolSet.add(objectOther1); // SAME value, but different object System.out.println(coolSet.size()); // → 2 (!!) — Shouldn't it be 1? // ⚠ WARNING: Without overriding equals() and hashCode(), // Java compares REFERENCES (memory address), not content. // object1 and objectOther1 have equal values but are different // objects → the Set treats them as "different".
Quick Reference
| Operation | Symbol | Meaning |
|---|---|---|
| Union | A ∪ B | Everything in A or B (or both) |
| Intersection | A ∩ B | Only what is in both A and B |
| Difference | A \ B | What is in A but not in B |
| Complement | A' | Everything in the universe not in A |
| Comp. of Union | (A ∪ B)' | Everything not in either set |
Practice Exercises
Solve using the operations you learned
Set<Integer> numbers = new HashSet<>(); numbers.add(3); numbers.add(3); System.out.println(numbers.size());
Answers
References
Sources used to build this material
Happy studying!
Set Theory is the foundation of computational logic