-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathSortListDemo.java
More file actions
52 lines (35 loc) · 1.39 KB
/
SortListDemo.java
File metadata and controls
52 lines (35 loc) · 1.39 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
package com.javatechie.stream.sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.javatechie.stream.api.example.DataBase;
import com.javatechie.stream.api.example.Employee;
public class SortListDemo {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(8);
list.add(3);
list.add(12);
list.add(4);
List<Employee> employees = DataBase.getEmployees();
/*Collections.sort(employees, new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return (int) (o1.getSalary() - o2.getSalary());// ascending
}
});*/
Collections.sort(employees, ( o1, o2) ->(int) (o1.getSalary() - o2.getSalary()));
//System.out.println(employees);
//employees.stream().sorted(( o1, o2) ->(int) (o2.getSalary() - o1.getSalary())).forEach(System.out::println);
//employees.stream().sorted(Comparator.comparing(emp->emp.getSalary())).forEach(System.out::println);
employees.stream().sorted(Comparator.comparing(Employee::getDept)).forEach(System.out::println);
/*
* Collections.sort(list);//ASSENDING Collections.reverse(list);
* System.out.println(list);
*
* list.stream().sorted(Comparator.reverseOrder()).forEach(s->System.out.println
* (s));//descending
*/
}
}