forked from tarunsinghofficial/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hash set and Tree set
86 lines (76 loc) · 2.08 KB
/
Hash set and Tree set
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
packagePBL_Lab;
importjava.util.*;
publicclass exp_4 {
exp_4()
{
HashSet hs=newHashSet();
hs.add("amrit");
hs.add("pal");
hs.add("singh");
hs.add("Sandhu");
System.out.print("elemenst in set\n"+hs);
//remove specific element from hashset
hs.remove("Sandhu");
System.out.print("\nAfter removing Sandhu from set\n"+hs);
//another set
HashSet hs1=newHashSet();
hs1.add("Sandhu");
hs1.add("CSE");
hs1.add("Chandigarh");
hs1.add("University");
System.out.print("\nset item\n"+hs1);
//cancatenate two sets
hs.addAll(hs1);
System.out.print("\nAftercancatenate two sets"+hs);
//use of iterator
System.out.print("\nTraversing item using iterator\n");
Iterator it=hs.iterator();
while(it.hasNext())
{
System.out.print(it.next()+" ");
}
//remove set 2 from set 1
hs.removeAll(hs1);
System.out.print("\nafter remove second set\n"+hs);
//clear complete set
hs.clear();
System.out.print("\nafter clear set"+hs);
System.out.print("\n------------------------------------------------------methods of tree set------------------------------------------------------\n");
//use of tree set
TreeSetts=newTreeSet();
ts.add("amrit");
ts.add("pal");
ts.add("singh");
ts.add("Sandhu");
System.out.print("elemenst in set\n"+hs);
//remove specific element from treeset
hs.remove("Sandhu");
System.out.print("\nAfter removing Sandhu from set\n"+ts);
//another set
TreeSetts1=newTreeSet();
ts1.add("Sandhu");
ts1.add("CSE");
ts1.add("Chandigarh");
ts1.add("University");
System.out.print("\nset item\n"+ts1);
//cancatenate two sets
ts.addAll(ts1);
System.out.print("\nAftercancatenate two sets"+ts);
//use of iterator
System.out.print("\nTraversing item using iterator\n");
Iterator it1=ts.iterator();
while(it1.hasNext())
{
System.out.print(it1.next()+" ");
}
//remove set 2 from set 1
ts.removeAll(ts1);
System.out.print("\nafter remove second set\n"+ts);
//clear complete set
ts.clear();
System.out.print("\nafter clear set"+ts);
}
publicstaticvoidmain(String agrs[]) {
new exp_4();
}
}