-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_TreeSet1.java
95 lines (75 loc) · 2.64 KB
/
Test_TreeSet1.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
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
86
87
88
89
90
91
92
93
94
95
package in.set;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.TreeSet;
public class Test_TreeSet1
{
public static void main(String[] args)
{
//TreeSet_Methods
TreeSet<Integer> ts1 = new TreeSet<Integer>();
ts1.add(45);
ts1.add(2);
ts1.add(4);
ts1.add(56);
ts1.remove(45);
System.out.println("TreeSet 1 : "+ts1);
TreeSet<Integer> ts2 = new TreeSet<Integer>();
ts2.add(3);
ts2.add(31);
ts2.add(78);
ts2.add(43);
System.out.println("TreeSet 2 : "+ts2);
System.out.println("\n");
TreeSet<Integer> ts3 = new TreeSet<Integer>();
ts1.addAll(ts2);
ts3.addAll(ts1);
System.out.println("ts1 : " + ts1);
System.out.println("ts2 : "+ ts2);
System.out.println("ts3 : "+ts3);
System.out.println("\n");
System.out.println("Ceiling : "+ts3.ceiling(50));
System.out.println("Floor : "+ ts3.floor(30));
System.out.println("\n");
System.out.println("Descending Iterator");
Iterator Descitr = ts3.descendingIterator();
while(Descitr.hasNext())
{
System.out.print(Descitr.next()+ " ");
}
System.out.println("\n"+"Ascending Iterator");
Iterator itr = ts3.iterator();
while(itr.hasNext())
{
System.out.print(itr.next()+ " ");
}
System.out.println("\n");
System.out.println("Reverse order : "+ts3.descendingSet());
System.out.println("Sorted HeadSet : "+ts3.headSet(43)); //Returns the set of values less than the specified number
System.out.println("Higher : "+ts3.higher(56));
System.out.println("Lower : " + ts3.lower(31));
System.out.println("=======================================");
System.out.println(ts3);
System.out.println("Lowest Element : "+ ts3.pollFirst());
System.out.println(ts3);
System.out.println("=======================================");
System.out.println(ts3);
System.out.println("Highest Element : " + ts3.pollLast());
System.out.println(ts3);
System.out.println("=======================================");
Spliterator<Integer> str = ts3.spliterator();
str.forEachRemaining(n-> System.out.println("Square of "+n+" : "+n*n));
System.out.println("\n");
System.out.println("Range : "+ts3.subSet(2, 20));
boolean isContaining = ts3.contains(4);
System.out.println(isContaining);
boolean isEmpty = ts3.isEmpty();
System.out.println(isEmpty);
boolean isRemoved = ts3.remove(56);
System.out.println(isRemoved + " " + ts3);
System.out.println("First element : "+ts3.first());
System.out.println("Last element : "+ts3.last());
int size = ts3.size();
System.out.println("Size : "+size);
}
}