-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathHashSetExample.java
44 lines (32 loc) · 1.01 KB
/
HashSetExample.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
// Import the HashMap class
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
// Create a HashSet object called set
HashSet<String> set = new HashSet<>();
// Add items to set
set.add("London");
set.add("Brest");
set.add("Berlin");
// Print items
System.out.println(set);
// Attempt to add London twice
set.add("London");
// Print values, London is not duplicated
System.out.println(set);
// To Remove An Item
set.remove("London");
// To find out the number of items
System.out.println(set.size());
// Looping through the items
for (String i : set) {
System.out.println(i);
}
// Is set empty or not
System.out.println(set.isEmpty());
// Verify if an element is present
System.out.println(set.contains("Paris"));
// To remove all items
set.clear();
}
}