-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetInterface.java
87 lines (66 loc) · 2.97 KB
/
SetInterface.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.*;
public class SetInterface {
public static void main(String[] args) {
// Create a set
Set<String> set = new HashSet<>();
// Add elements
set.add("Apple");
set.add("Banana");
set.add("Cherry");
// Demonstrate basic operations
demonstrateBasicOperations(set);
// Demonstrate advanced operations
demonstrateAdvancedOperations(set);
}
private static void demonstrateBasicOperations(Set<String> set) {
System.out.println("Basic Set Operations:");
// Check size
System.out.println("Size of the set: " + set.size());
// Check if an element exists
System.out.println("Contains 'Banana': " + set.contains("Banana"));
// Remove an element
set.remove("Banana");
System.out.println("Set after removal: " + set);
// Iterate over elements
System.out.println("Set elements:");
for (String fruit : set) {
System.out.println(fruit);
}
// Clear the set
set.clear();
System.out.println("Is set empty? " + set.isEmpty());
System.out.println(); // Empty line for better readability
}
private static void demonstrateAdvancedOperations(Set<String> set) {
System.out.println("Advanced Set Operations:");
// Add elements again for demonstration
set.addAll(Arrays.asList("Apple", "Banana", "Cherry", "Date", "Elderberry"));
// Check if the set contains all elements of another collection
Set<String> subset = new HashSet<>(Arrays.asList("Banana", "Cherry"));
System.out.println("Set contains all elements of subset: " + set.containsAll(subset));
// Remove all elements in another collection
set.removeAll(subset);
System.out.println("Set after removing elements of subset: " + set);
// Retain only elements in another collection
Set<String> retainSet = new HashSet<>(Arrays.asList("Apple", "Date"));
set.retainAll(retainSet);
System.out.println("Set after retaining elements of retainSet: " + set);
// Add elements back
set.addAll(Arrays.asList("Banana", "Cherry", "Elderberry"));
// Convert set to array
Object[] array = set.toArray();
System.out.println("Array from set: " + Arrays.toString(array));
// Check equality with another set
Set<String> anotherSet = new HashSet<>(Arrays.asList("Apple", "Banana", "Cherry", "Date", "Elderberry"));
System.out.println("Sets are equal: " + set.equals(anotherSet));
// Calculate hash code
System.out.println("Set hash code: " + set.hashCode());
// Iterate using Iterator
System.out.println("Iterating with Iterator:");
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println(); // Empty line for better readability
}
}