forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparatorTest.java
More file actions
83 lines (73 loc) · 2.29 KB
/
ComparatorTest.java
File metadata and controls
83 lines (73 loc) · 2.29 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
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
package org.tron.common;
import com.google.common.collect.ImmutableList;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.testng.collections.Lists;
import java.util.Comparator;
import java.util.List;
@Slf4j
public class ComparatorTest {
@Test
public void intComparator() {
List<Integer> list1 = Lists.newArrayList(ImmutableList.of(1,8,4,6,2));
List<Integer> list2 = Lists.newArrayList(ImmutableList.of(1,8,4,6,2));
list1.sort((o1, o2) -> o1 - o2);
list2.sort(Comparator.comparingInt(o -> o));
logger.info("list1:" + list1);
logger.info("list2:" + list2);
Assert.assertEquals(list1, list2);
List<Integer> list3 = Lists.newArrayList(ImmutableList.of(1,8,4,6,2));
List<Integer> list4 = Lists.newArrayList(ImmutableList.of(1,8,4,6,2));
List<Integer> list5 = Lists.newArrayList(ImmutableList.of(1,8,4,6,2));
list3.sort((o1, o2) -> o2 - o1);
list4.sort(Comparator.comparingInt((Integer o) -> o).reversed());
list5.sort((o1, o2) -> -(o1 - o2));
logger.info("list3:" + list3);
logger.info("list4:" + list4);
logger.info("list5:" + list5);
Assert.assertEquals(list3, list4);
Assert.assertEquals(list3, list5);
}
@Test
public void thenComparing() {
C c1 = new C(1, new B(20));
C c2 = new C(2, new B(40));
C c3 = new C(3, new B(60));
C c4 = new C(4, new B(10));
C c5 = new C(5, new B(30));
C c6 = new C(5, new B(0));
List<C> cs1 = Lists.newArrayList(c4, c5, c1, c3, c2, c6);
List<C> cs2 = Lists.newArrayList(c4, c5, c1, c3, c2, c6);
logger.info("cs1:" + cs1);
logger.info("cs2:" + cs2);
cs1.sort((a, b) -> {
int age1 = a.getAge();
int age2 = b.getAge();
if (age1 != age2) {
return age2 - age1;
} else {
return Long.compare(b.getB().getHigh(), a.getB().getHigh());
}
});
cs2.sort(Comparator.comparingInt((C c) -> c.getAge())
.reversed()
.thenComparing(Comparator.comparingInt((C c) -> c.getB().getHigh()).reversed()));
logger.info("cs1:" + cs1);
logger.info("cs2:" + cs2);
Assert.assertEquals(cs1, cs2);
}
@Data
@AllArgsConstructor
static class B {
int high;
}
@Data
@AllArgsConstructor
static class C {
int age;
B b;
}
}