-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetOperationsHashSet.java
More file actions
33 lines (25 loc) · 1.07 KB
/
Copy pathSetOperationsHashSet.java
File metadata and controls
33 lines (25 loc) · 1.07 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
package Interns_CoreJavaAssignments_V001;
import java.util.*;
public class SetOperationsHashSet {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Create a HashSet to store unique elements
HashSet<String> fruitSet = new HashSet<>();
// Adding initial items to the set
fruitSet.add("apple");
fruitSet.add("banana");
fruitSet.add("orange");
// Display the initial set
System.out.println("Initial Set: " + fruitSet);
// Removing an item from the set
fruitSet.remove("banana");
System.out.println("Set after removal: " + fruitSet);
// Checking if an item exists in the set
String checkItem = "orange";
boolean exists = fruitSet.contains(checkItem);
System.out.println("'" + checkItem + "' exists in the set: " + exists);
// Adding a new item to demonstrate the uniqueness property
fruitSet.add("apple"); // Attempt to add duplicate
System.out.println("Set after attempting to add duplicate 'apple': " + fruitSet);
}
}