-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetCustomSortingExample.java
More file actions
37 lines (33 loc) · 949 Bytes
/
SetCustomSortingExample.java
File metadata and controls
37 lines (33 loc) · 949 Bytes
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
package lambda_expressions.custom_sorting;
/*
* Program to demonstrate Customized Sorting Order(Descending order) for TreeSet.
*/
import java.util.Comparator;
import java.util.TreeSet;
/*
* Comparator is a Functional interface with SAM compare()
* interface<T> Comparator{
* public int compare(T obj1, T obj2);
* }
* return -ve if obj1 has to come before obj2
* return +e if obj1 has to come after obj2
* return 0 if obj1 and obj2 are equal.
*
*/
public class SetCustomSortingExample {
public static void main(String[] args) {
Comparator<Integer> comparator = (value1, value2) -> -value1.compareTo(value2);
TreeSet<Integer> ts = new TreeSet<>(comparator); //sorted based on passed comparator
ts.add(10);
ts.add(0);
ts.add(15);
ts.add(25);
ts.add(5);
ts.add(20);
System.out.println(ts);
}
}
/*
* Output:
* [25, 20, 15, 10, 5, 0]
*/