forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeSet_Example.java
More file actions
54 lines (31 loc) · 1.31 KB
/
TreeSet_Example.java
File metadata and controls
54 lines (31 loc) · 1.31 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
import java.util.TreeSet;
public class TreeSet_Example{
public static void main(String args[]) {
//The objects of the TreeSet class are stored in ascending order.
TreeSet<String> values = new TreeSet<String>();
values.add("Abhi");
values.add("Babita");
values.add("Chanchal");
values.add("Dhruv");
values.add("Ellen");
System.out.println("Initial Set: " + values); //Default storage in ascending order
System.out.println("Reverse Set: " + values.descendingSet()); //Storing in descending order
//Methods of Java TreeSet class
System.out.println("Head Set: " + values.headSet("Chanchal", false)); // //
System.out.println("SubSet: " + values.subSet("Abhi", false, "Ellen", true));
System.out.println("TailSet: " + values.tailSet("Chanchal", true));
//Retrieves and Removes the highest and lowest Value.
System.out.println("Highest Value: "+values.pollFirst());
System.out.println("Lowest Value: "+values.pollLast());
}
}
//OUTPUT :
/*
Initial Set: [Abhi, Babita, Chanchal, Dhruv, Ellen]
Reverse Set: [Ellen, Dhruv, Chanchal, Babita, Abhi]
Head Set: [Abhi, Babita]
SubSet: [Babita, Chanchal, Dhruv, Ellen]
TailSet: [Chanchal, Dhruv, Ellen]
Highest Value: Abhi
Lowest Value: Ellen
*/