-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareTest.java
More file actions
93 lines (72 loc) · 2.75 KB
/
Copy pathCompareTest.java
File metadata and controls
93 lines (72 loc) · 2.75 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
84
85
86
87
88
89
90
91
92
93
package compare.java;
import org.junit.Test;
import java.util.Arrays;
import java.util.Comparator;
/*
comparable vs comparator :
comparable interface implemented on the class that any instance would be able to compare anywhere
comparator is more like temporarily add a tool at compare method to comapre the things
*/
public class CompareTest {
/* String的包装类实现了comparable的接口, 可以重写compareTo(obj)方法
* 当前对象this大于形参类的对象,return positive value, vise versa,
* if returns 0, means they are equal on value
* */
@Test
public void test(){
String[] arr = new String[]{"AA", "ZZ", "LL", "QQ", "CC", "MM"};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
/* 自定义类,需要implements interface Comparable and override compareTo() */
@Test
public void test2(){
Product[] mouses = new Product[3];
mouses[0] = new Product("Dell", 300);
mouses[1] = new Product("Apple", 120);
mouses[2] = new Product("Lenovo", 120);
Arrays.sort(mouses);
System.out.println(Arrays.toString(mouses));
}
@Test
public void comparatorTest3(){
String[] arr = new String[]{"AA", "ZZ", "LL", "QQ", "CC", "MM"};
Arrays.sort(arr, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof String && o2 instanceof String){
String s1 = (String) o1;
String s2 = (String) o2;
return -s1.compareTo(s2);
}
throw new RuntimeException("unproper input type");
}
});
System.out.println(Arrays.toString(arr));
}
@Test
public void test3(){
Product[] mouses = new Product[5];
mouses[0] = new Product("Dell", 300);
mouses[1] = new Product("Apple", 120);
mouses[2] = new Product("Lenovo", 120);
mouses[3] = new Product("Huawei", 141);
mouses[4] = new Product("Huawei", 230);
Arrays.sort(mouses, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Product && o2 instanceof Product){
Product p1 = (Product) o1;
Product p2 = (Product) o2;
if(p1.getName().equals(p2.getName())){
return -Double.compare(p1.getPrice(), p2.getPrice());
}else{
return p1.getName().compareTo(p2.getName());
}
}
throw new RuntimeException("wrong type exception");
}
});
System.out.println(Arrays.toString(mouses));
}
}