- Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathHashSetSimpleOperationsExample.java
27 lines (22 loc) · 924 Bytes
/
HashSetSimpleOperationsExample.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
importjava.util.HashSet;
importjava.util.Set;
publicclassHashSetSimpleOperationsExample {
publicstaticvoidmain(String[] args) {
Set<String> popularCities = newHashSet<>();
// Check if a HashSet is empty
System.out.println("Is popularCities set empty? : " + popularCities.isEmpty());
popularCities.add("London");
popularCities.add("New York");
popularCities.add("Paris");
popularCities.add("Dubai");
// Find the size of a HashSet
System.out.println("Number of cities in the HashSet " + popularCities.size());
// Check if the HashSet contains an element
StringcityName = "Paris";
if(popularCities.contains(cityName)) {
System.out.println(cityName + " is in the popular cities set.");
} else {
System.out.println(cityName + " is not in the popular cities set.");
}
}
}