-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha61hashSet.java
More file actions
57 lines (52 loc) · 1.64 KB
/
a61hashSet.java
File metadata and controls
57 lines (52 loc) · 1.64 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
49
50
51
52
53
54
55
56
57
import java.util.*;
public class a61hashSet {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
// add operation
set.add(1);
set.add(2);
set.add(3);
set.add(2);
set.add(1);
set.add(5);
set.add(9);
System.out.println(set);
// remove operation
// set.remove(2);
// System.out.println(set);
// contains operation
if (set.contains(2)) {
System.out.println("Element Present");
}else{
System.out.println("Not Present");
}
//size opr & isEmpty operation
// System.out.println(set.size());
// System.out.println(set.isEmpty());
// Iteration on hashSet
// 1 - Using Iterator
// starts from null and goes on till end it.next() checks for next value if next value exists then print and store that value in it
Iterator it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
// 2 - using advance loop
for (Integer no : set) {
System.out.println(no);
}
LinkedHashSet<String> lhs = new LinkedHashSet<>();
// Maintains order
lhs.add("India");
lhs.add("Aus");
lhs.add("Rus");
lhs.add("China");
System.out.println(lhs);
TreeSet<String> ts = new TreeSet<>();
// Maintains sorting order
ts.add("India");
ts.add("Aus");
ts.add("Rus");
ts.add("China");
System.out.println(ts);
}
}