forked from onlybooks/java-algorithm-interview
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLambdaExpressionSortExample.java
56 lines (45 loc) · 1.46 KB
/
LambdaExpressionSortExample.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
package ch02;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class LambdaExpressionSortExample {
static class Member {
String name;
int age;
public Member(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Member{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public static void main(String[] args) {
// 리스트에 데이터 삽입
List<Member> members1 = new ArrayList<>();
members1.add(new Member("윈터", 23));
members1.add(new Member("카리나", 24));
members1.add(new Member("닝닝", 22));
// 정렬을 익명 클래스로 구현
Collections.sort(members1, new Comparator<Member>() {
@Override
public int compare(Member o1, Member o2) {
return o2.age - o1.age;
}
});
System.out.println(members1);
// ---
List<Member> members2 = new ArrayList<>();
members2.add(new Member("윈터", 23));
members2.add(new Member("카리나", 24));
members2.add(new Member("닝닝", 22));
// 정렬을 람다 표현식으로 구현
members2.sort((o1, o2) -> o2.age - o1.age);
System.out.println(members2);
}
}