Repository navigation
Expand file tree
/
Copy pathSetInterfaceDemo.java
More file actions
47 lines (40 loc) · 1.64 KB
/
Copy pathSetInterfaceDemo.java
File metadata and controls
47 lines (40 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package javakb.ch12_collections.set;
import java.util.HashSet;
import java.util.Set;
/**
* Demonstrates the core capabilities of the java.util.Set interface.
* Key Concepts:
* 1. Collection that contains no duplicate elements.
* 2. Models the mathematical set abstraction.
* 3. Does not guarantee order (behavior depends on implementation).
* 4. Demonstrates set operations: Union, Intersection, and Difference.
*/
public class SetInterfaceDemo {
public static void main(String[] args) {
// 1. Defining two sets for mathematical set operations
Set<Integer> setA = new HashSet<>();
setA.add(1);
setA.add(2);
setA.add(3);
setA.add(4);
Set<Integer> setB = new HashSet<>();
setB.add(3);
setB.add(4);
setB.add(5);
setB.add(6);
System.out.println("Set A: " + setA);
System.out.println("Set B: " + setB);
// 2. UNION (All elements from both sets)
Set<Integer> union = new HashSet<>(setA);
union.addAll(setB); // Performs Union operation
System.out.println("Union (A U B): " + union); // [1, 2, 3, 4, 5, 6]
// 3. INTERSECTION (Common elements in both sets)
Set<Integer> intersection = new HashSet<>(setA);
intersection.retainAll(setB); // Performs Intersection operation
System.out.println("Intersection (A n B): " + intersection); // [3, 4]
// 4. DIFFERENCE (Elements in A but not in B)
Set<Integer> difference = new HashSet<>(setA);
difference.removeAll(setB); // Performs Difference operation
System.out.println("Difference (A - B): " + difference); // [1, 2]
}
}