-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearnHashSet.java
More file actions
48 lines (31 loc) · 1.12 KB
/
LearnHashSet.java
File metadata and controls
48 lines (31 loc) · 1.12 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
48
// HashSet is used to store the keys.
import java.util.*;
public class LearnHashSet {
public static void main(String[] args) {
// We can specify the initial capacity of the HashSet in the circular brackets;
HashSet<Integer> myHashSet = new HashSet<>();
myHashSet.add(5);
myHashSet.add(8);
myHashSet.add(7);
myHashSet.add(11);
// add function will return false if the item is already present and will ignore the add command
myHashSet.add(7);
System.out.println(myHashSet);
System.out.println(myHashSet.contains(5));
Iterator<Integer> i = myHashSet.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
System.out.println(myHashSet.size());
myHashSet.remove(8);
System.out.println(myHashSet.size());
for(Integer j : myHashSet)
{
System.out.println(j);
}
System.out.println(myHashSet.isEmpty());
myHashSet.clear();
System.out.println(myHashSet.isEmpty());
}
}