diff --git a/1940/10/24/hello-world/index.html b/1940/10/24/hello-world/index.html index 501e01b..5bf9aa6 100644 --- a/1940/10/24/hello-world/index.html +++ b/1940/10/24/hello-world/index.html @@ -469,7 +469,7 @@

@@ -483,7 +483,7 @@

diff --git a/2015/06/18/A-Simple-Video/index.html b/2015/06/18/A-Simple-Video/index.html index 2ddd2dd..93439f8 100644 --- a/2015/06/18/A-Simple-Video/index.html +++ b/2015/06/18/A-Simple-Video/index.html @@ -470,7 +470,7 @@

@@ -484,7 +484,7 @@

diff --git a/2015/06/18/firstblog/index.html b/2015/06/18/firstblog/index.html index 809220b..4064b4f 100644 --- a/2015/06/18/firstblog/index.html +++ b/2015/06/18/firstblog/index.html @@ -470,7 +470,7 @@

Talk is cheap,show me the code.

@@ -484,7 +484,7 @@

Talk is cheap,show me the code.

diff --git a/2015/06/22/readingList/index.html b/2015/06/22/readingList/index.html index fd6e9aa..40985f5 100644 --- a/2015/06/22/readingList/index.html +++ b/2015/06/22/readingList/index.html @@ -479,7 +479,7 @@

@@ -493,7 +493,7 @@

diff --git a/2015/06/23/java-Long-to-Integer/index.html b/2015/06/23/java-Long-to-Integer/index.html index 5758b20..5b9e621 100644 --- a/2015/06/23/java-Long-to-Integer/index.html +++ b/2015/06/23/java-Long-to-Integer/index.html @@ -472,7 +472,7 @@

@@ -486,7 +486,7 @@

diff --git "a/2015/06/24/\345\255\227\347\254\246\344\270\262\347\264\242\345\274\225\350\266\212\347\225\214\345\274\202\345\270\270/index.html" "b/2015/06/24/\345\255\227\347\254\246\344\270\262\347\264\242\345\274\225\350\266\212\347\225\214\345\274\202\345\270\270/index.html" index 9a0be4b..393bfef 100644 --- "a/2015/06/24/\345\255\227\347\254\246\344\270\262\347\264\242\345\274\225\350\266\212\347\225\214\345\274\202\345\270\270/index.html" +++ "b/2015/06/24/\345\255\227\347\254\246\344\270\262\347\264\242\345\274\225\350\266\212\347\225\214\345\274\202\345\270\270/index.html" @@ -473,7 +473,7 @@

@@ -487,7 +487,7 @@

diff --git "a/2015/06/25/Java-\346\257\224\350\276\203\345\231\250/index.html" "b/2015/06/25/Java-\346\257\224\350\276\203\345\231\250/index.html" index 091f877..e15c962 100644 --- "a/2015/06/25/Java-\346\257\224\350\276\203\345\231\250/index.html" +++ "b/2015/06/25/Java-\346\257\224\350\276\203\345\231\250/index.html" @@ -496,7 +496,7 @@

引用

@@ -510,7 +510,7 @@
引用

- 9 + 10 标签

diff --git "a/2015/06/25/Java\344\270\255\345\255\220\347\261\273\345\222\214\347\210\266\347\261\273\347\232\204\346\236\204\351\200\240\345\207\275\346\225\260/index.html" "b/2015/06/25/Java\344\270\255\345\255\220\347\261\273\345\222\214\347\210\266\347\261\273\347\232\204\346\236\204\351\200\240\345\207\275\346\225\260/index.html" index fbbb5fd..1f3a149 100644 --- "a/2015/06/25/Java\344\270\255\345\255\220\347\261\273\345\222\214\347\210\266\347\261\273\347\232\204\346\236\204\351\200\240\345\207\275\346\225\260/index.html" +++ "b/2015/06/25/Java\344\270\255\345\255\220\347\261\273\345\222\214\347\210\266\347\261\273\347\232\204\346\236\204\351\200\240\345\207\275\346\225\260/index.html" @@ -494,7 +494,7 @@

@@ -508,7 +508,7 @@

diff --git "a/2015/06/27/java\344\270\255\345\274\200\345\217\221\350\200\205\345\246\202\344\275\225\346\216\222\345\272\217/index.html" "b/2015/06/27/java\344\270\255\345\274\200\345\217\221\350\200\205\345\246\202\344\275\225\346\216\222\345\272\217/index.html" index d654205..c0be819 100644 --- "a/2015/06/27/java\344\270\255\345\274\200\345\217\221\350\200\205\345\246\202\344\275\225\346\216\222\345\272\217/index.html" +++ "b/2015/06/27/java\344\270\255\345\274\200\345\217\221\350\200\205\345\246\202\344\275\225\346\216\222\345\272\217/index.html" @@ -411,6 +411,8 @@

不好的实例

有很多不好的实例,比如

+ Comparator vs Comparable +
@@ -481,7 +483,7 @@

不好的实例

有很多不好的实例,比如

@@ -495,7 +497,7 @@

不好的实例

有很多不好的实例,比如

diff --git a/2015/06/29/Comparator-vs-Comparable/index.html b/2015/06/29/Comparator-vs-Comparable/index.html new file mode 100644 index 0000000..0a86a10 --- /dev/null +++ b/2015/06/29/Comparator-vs-Comparable/index.html @@ -0,0 +1,803 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Comparator vs Comparable // Courage Me + + + + + +
+
+ + + +
+
+
+ + +
+ + +
+
+ + + +

+ + + + Comparator vs Comparable + + +

+ + + +
+ +
+ + + + + +

Comparable 和 Comparator 是JAVA 核心API 提供的两个接口。从它们的名字我们可以得知它们是用来比较东西的在某种意义上说。但他们到底是什么,它们之间的区别是什么呢?以下是回答这个问题的2个例子。比较两个HDTV的尺寸简单的例子。在阅读代码后,如何使用Comparable vs. Comparator就比较容易了。

+

1.Comparable

一个类实现Comparable接口,以便能够比较对象本身与一些其他对象。类本身必须实现此接口以便能够比较其实例(的)。所需实现的方法compareto()。这里就是一个例子:

+
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
class HDTV implements Comparable<HDTV> {
private int size;
private String brand;

public HDTV(int size, String brand) {
this.size = size;
this.brand = brand;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

@Override
public int compareTo(HDTV tv) {

if (this.getSize() > tv.getSize())
return 1;
else if (this.getSize() < tv.getSize())
return -1;
else
return 0;
}
}

public class Main {
public static void main(String[] args) {
HDTV tv1 = new HDTV(55, "Samsung");
HDTV tv2 = new HDTV(60, "Sony");

if (tv1.compareTo(tv2) > 0) {
System.out.println(tv1.getBrand() + " is better.");
} else {
System.out.println(tv2.getBrand() + " is better.");
}
}
}
+
+

Sony is better.

+
+

2.Comparator

在某些情况下,您可能不希望更改一个类,并使其具有可比较性。在这种情况下,如果你想比较基于某些属性/字段的对象,可以使用Comparator。例如,2个人可以基础上的身高或年龄等进行比较(Comparable不能被用于这种比较)。

+

需要实现的方法是compare()。现在让我们用另一种方式来比较这些电视的大小。比较器的一个常见用途是排序。集合和数组类提供了一种使用比较器的排序方法。

+
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class HDTV {
private int size;
private String brand;

public HDTV(int size, String brand) {
this.size = size;
this.brand = brand;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}
}

class SizeComparator implements Comparator<HDTV> {
@Override
public int compare(HDTV tv1, HDTV tv2) {
int tv1Size = tv1.getSize();
int tv2Size = tv2.getSize();

if (tv1Size > tv2Size) {
return 1;
} else if (tv1Size < tv2Size) {
return -1;
} else {
return 0;
}
}
}

public class Main {
public static void main(String[] args) {
HDTV tv1 = new HDTV(55, "Samsung");
HDTV tv2 = new HDTV(60, "Sony");
HDTV tv3 = new HDTV(42, "Panasonic");

ArrayList<HDTV> al = new ArrayList<HDTV>();
al.add(tv1);
al.add(tv2);
al.add(tv3);

Collections.sort(al, new SizeComparator());
for (HDTV a : al) {
System.out.println(a.getBrand());
}
}
}
+
+

Panasonic
Samsung
Sony

+
+

我们经常会使用Collections.reverseorder()方法得到一个降序比较器。如下:

+
1
2
3
4
5
6
7
8
9
10
11
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(3);
al.add(1);
al.add(2);
System.out.println(al);
Collections.sort(al);
System.out.println(al);

Comparator<Integer> comparator = Collections.reverseOrder();
Collections.sort(al,comparator);
System.out.println(al);
+

Output:

+
+

[3,1,2]
[1,2,3]
[3,2,1]

+
+

3.什么时候使用?

简单地说,一个类实现Comparable可以被比性,这意味着它的实例之间可以相互比较。
一个类实现Comparator将主要在两种情况下使用:1)它可以通过一个排序方法,如Collections.sort()或Arrays.sort(),允许精确控制的排序顺序和2)它也可以被用来控制的顺序一定的数据结构,如有序集合(例如TreeSet)或排序图(例如,Treemap)。

+

例如,创建一个TreeSet。我们可以通过构造一个Comparator或使对象类可比较。

+

方法1——TreeSet(Comparator comparator)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Dog {
int size;

Dog(int s) {
size = s;
}
}

class SizeComparator implements Comparator<Dog> {
@Override
public int compare(Dog d1, Dog d2) {
return d1.size - d2.size;
}
}

public class ImpComparable {
public static void main(String[] args) {
TreeSet<Dog> d = new TreeSet<Dog>(new SizeComparator()); // pass comparator
d.add(new Dog(1));
d.add(new Dog(2));
d.add(new Dog(1));
}
}

+

方法2——实现Comparable接口

+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Dog implements Comparable<Dog>{
int size;

Dog(int s) {
size = s;
}

@Override
public int compareTo(Dog o) {
return o.size - this.size;
}
}

public class ImpComparable {
public static void main(String[] args) {
TreeSet<Dog> d = new TreeSet<Dog>();
d.add(new Dog(1));
d.add(new Dog(2));
d.add(new Dog(1));
}
}
+

原文链接

+ + +
+ + +
+ + + + + + +
+ +
+
+ +
+ +
+ + +
+ + +
+ + + + + + + + + +
+ + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/about/index.html b/about/index.html index 226a4a1..000645a 100644 --- a/about/index.html +++ b/about/index.html @@ -370,7 +370,7 @@

ABOUT ME

@@ -384,7 +384,7 @@

ABOUT ME

diff --git a/archives/1940/10/index.html b/archives/1940/10/index.html index 82aea3b..151e2e9 100644 --- a/archives/1940/10/index.html +++ b/archives/1940/10/index.html @@ -335,7 +335,7 @@

- 嗯..! 目前共计 9 篇日志。 继续努力。 + 嗯..! 目前共计 10 篇日志。 继续努力。 @@ -411,7 +411,7 @@

1940

@@ -425,7 +425,7 @@

1940

diff --git a/archives/1940/index.html b/archives/1940/index.html index 82aea3b..151e2e9 100644 --- a/archives/1940/index.html +++ b/archives/1940/index.html @@ -335,7 +335,7 @@

- 嗯..! 目前共计 9 篇日志。 继续努力。 + 嗯..! 目前共计 10 篇日志。 继续努力。 @@ -411,7 +411,7 @@

1940

@@ -425,7 +425,7 @@

1940

diff --git a/archives/2015/06/index.html b/archives/2015/06/index.html index 47be881..119974e 100644 --- a/archives/2015/06/index.html +++ b/archives/2015/06/index.html @@ -335,7 +335,7 @@

- 嗯..! 目前共计 9 篇日志。 继续努力。 + 嗯..! 目前共计 10 篇日志。 继续努力。 @@ -354,6 +354,37 @@

2015

+
+
+ +
+ + + Comparator vs Comparable + + +
+ + + +
+
+ + + + + + + + + + + + + +
@@ -628,7 +659,7 @@

2015

@@ -642,7 +673,7 @@

2015

diff --git a/archives/2015/index.html b/archives/2015/index.html index 47be881..119974e 100644 --- a/archives/2015/index.html +++ b/archives/2015/index.html @@ -335,7 +335,7 @@

- 嗯..! 目前共计 9 篇日志。 继续努力。 + 嗯..! 目前共计 10 篇日志。 继续努力。 @@ -354,6 +354,37 @@

2015

+
+
+ +
+ + + Comparator vs Comparable + + +
+ + + +
+
+ + + + + + + + + + + + + +
@@ -628,7 +659,7 @@

2015

@@ -642,7 +673,7 @@

2015

diff --git a/archives/index.html b/archives/index.html index c823c44..fd1946e 100644 --- a/archives/index.html +++ b/archives/index.html @@ -335,7 +335,7 @@

- 嗯..! 目前共计 9 篇日志。 继续努力。 + 嗯..! 目前共计 10 篇日志。 继续努力。 @@ -354,6 +354,37 @@

2015

+
+
+ +
+ + + Comparator vs Comparable + + +
+ + + +
+
+ + + + + + + + + + + + + +
@@ -664,7 +695,7 @@

1940

@@ -678,7 +709,7 @@

1940

diff --git a/atom.xml b/atom.xml index 02dc7a2..ed74dfb 100644 --- a/atom.xml +++ b/atom.xml @@ -5,7 +5,7 @@ - 2015-06-27T15:38:16.000Z + 2015-06-29T15:48:58.000Z http://yoursite.com// @@ -15,6 +15,47 @@ Hexo + + <![CDATA[Comparator vs Comparable]]> + + http://yoursite.com/2015/06/29/Comparator-vs-Comparable/ + 2015-06-29T15:44:28.000Z + 2015-06-29T15:48:58.000Z + Comparable 和 Comparator 是JAVA 核心API 提供的两个接口。从它们的名字我们可以得知它们是用来比较东西的在某种意义上说。但他们到底是什么,它们之间的区别是什么呢?以下是回答这个问题的2个例子。比较两个HDTV的尺寸简单的例子。在阅读代码后,如何使用Comparable vs. Comparator就比较容易了。

+

1.Comparable

一个类实现Comparable接口,以便能够比较对象本身与一些其他对象。类本身必须实现此接口以便能够比较其实例(的)。所需实现的方法compareto()。这里就是一个例子:

+
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
class HDTV implements Comparable<HDTV> {
private int size;
private String brand;

public HDTV(int size, String brand) {
this.size = size;
this.brand = brand;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

@Override
public int compareTo(HDTV tv) {

if (this.getSize() > tv.getSize())
return 1;
else if (this.getSize() < tv.getSize())
return -1;
else
return 0;
}
}

public class Main {
public static void main(String[] args) {
HDTV tv1 = new HDTV(55, "Samsung");
HDTV tv2 = new HDTV(60, "Sony");

if (tv1.compareTo(tv2) > 0) {
System.out.println(tv1.getBrand() + " is better.");
} else {
System.out.println(tv2.getBrand() + " is better.");
}
}
}
+
+

Sony is better.

+
+

2.Comparator

在某些情况下,您可能不希望更改一个类,并使其具有可比较性。在这种情况下,如果你想比较基于某些属性/字段的对象,可以使用Comparator。例如,2个人可以基础上的身高或年龄等进行比较(Comparable不能被用于这种比较)。

+

需要实现的方法是compare()。现在让我们用另一种方式来比较这些电视的大小。比较器的一个常见用途是排序。集合和数组类提供了一种使用比较器的排序方法。

+
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class HDTV {
private int size;
private String brand;

public HDTV(int size, String brand) {
this.size = size;
this.brand = brand;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}
}

class SizeComparator implements Comparator<HDTV> {
@Override
public int compare(HDTV tv1, HDTV tv2) {
int tv1Size = tv1.getSize();
int tv2Size = tv2.getSize();

if (tv1Size > tv2Size) {
return 1;
} else if (tv1Size < tv2Size) {
return -1;
} else {
return 0;
}
}
}

public class Main {
public static void main(String[] args) {
HDTV tv1 = new HDTV(55, "Samsung");
HDTV tv2 = new HDTV(60, "Sony");
HDTV tv3 = new HDTV(42, "Panasonic");

ArrayList<HDTV> al = new ArrayList<HDTV>();
al.add(tv1);
al.add(tv2);
al.add(tv3);

Collections.sort(al, new SizeComparator());
for (HDTV a : al) {
System.out.println(a.getBrand());
}
}
}
+
+

Panasonic
Samsung
Sony

+
+

我们经常会使用Collections.reverseorder()方法得到一个降序比较器。如下:

+
1
2
3
4
5
6
7
8
9
10
11
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(3);
al.add(1);
al.add(2);
System.out.println(al);
Collections.sort(al);
System.out.println(al);

Comparator<Integer> comparator = Collections.reverseOrder();
Collections.sort(al,comparator);
System.out.println(al);
+

Output:

+
+

[3,1,2]
[1,2,3]
[3,2,1]

+
+

3.什么时候使用?

简单地说,一个类实现Comparable可以被比性,这意味着它的实例之间可以相互比较。
一个类实现Comparator将主要在两种情况下使用:1)它可以通过一个排序方法,如Collections.sort()或Arrays.sort(),允许精确控制的排序顺序和2)它也可以被用来控制的顺序一定的数据结构,如有序集合(例如TreeSet)或排序图(例如,Treemap)。

+

例如,创建一个TreeSet。我们可以通过构造一个Comparator或使对象类可比较。

+

方法1——TreeSet(Comparator comparator)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Dog {
int size;

Dog(int s) {
size = s;
}
}

class SizeComparator implements Comparator<Dog> {
@Override
public int compare(Dog d1, Dog d2) {
return d1.size - d2.size;
}
}

public class ImpComparable {
public static void main(String[] args) {
TreeSet<Dog> d = new TreeSet<Dog>(new SizeComparator()); // pass comparator
d.add(new Dog(1));
d.add(new Dog(2));
d.add(new Dog(1));
}
}

+

方法2——实现Comparable接口

+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Dog implements Comparable<Dog>{
int size;

Dog(int s) {
size = s;
}

@Override
public int compareTo(Dog o) {
return o.size - this.size;
}
}

public class ImpComparable {
public static void main(String[] args) {
TreeSet<Dog> d = new TreeSet<Dog>();
d.add(new Dog(1));
d.add(new Dog(2));
d.add(new Dog(1));
}
}
+

原文链接

+]]>
+ + Comparable 和 Comparator 是JAVA 核心API 提供的两个接口。从它们的名字我们可以得知它们是用来比较东西的在某种意义上说。但他们到底是什么,它们之间的区别是什么呢?以下是回答这个问题的2个例子。比较两个HDTV的尺寸简单的例子。在阅读代码后,如何使]]> + + + + + + +
+ <![CDATA[java中开发者如何排序]]> diff --git a/categories/Classical-Fragment/index.html b/categories/Classical-Fragment/index.html index 4f19737..0f12683 100644 --- a/categories/Classical-Fragment/index.html +++ b/categories/Classical-Fragment/index.html @@ -392,7 +392,7 @@

@@ -406,7 +406,7 @@

diff --git a/categories/Simple-Java/index.html b/categories/Simple-Java/index.html index d466da1..c13b042 100644 --- a/categories/Simple-Java/index.html +++ b/categories/Simple-Java/index.html @@ -337,6 +337,28 @@

+
+
+ +
+ + + Comparator vs Comparable + + +
+ + + +
+
+ + + + +
@@ -414,7 +436,7 @@

@@ -428,7 +450,7 @@

diff --git a/categories/index.html b/categories/index.html index 74beb2c..30be79b 100644 --- a/categories/index.html +++ b/categories/index.html @@ -340,7 +340,7 @@

目前共计 5 个分类

@@ -377,7 +377,7 @@

@@ -391,7 +391,7 @@

diff --git a/categories/java/index.html b/categories/java/index.html index c51dc41..04b8ad5 100644 --- a/categories/java/index.html +++ b/categories/java/index.html @@ -436,7 +436,7 @@

@@ -450,7 +450,7 @@

diff --git a/categories/start/index.html b/categories/start/index.html index 1a78a5a..bfc50bb 100644 --- a/categories/start/index.html +++ b/categories/start/index.html @@ -414,7 +414,7 @@

@@ -428,7 +428,7 @@

diff --git "a/categories/\350\257\273\344\271\246\346\270\205\345\215\225/index.html" "b/categories/\350\257\273\344\271\246\346\270\205\345\215\225/index.html" index 86ccd99..cba0cbd 100644 --- "a/categories/\350\257\273\344\271\246\346\270\205\345\215\225/index.html" +++ "b/categories/\350\257\273\344\271\246\346\270\205\345\215\225/index.html" @@ -392,7 +392,7 @@

@@ -406,7 +406,7 @@

diff --git a/css/main.css b/css/main.css index 9dbd92c..d6f3056 100644 --- a/css/main.css +++ b/css/main.css @@ -1197,7 +1197,7 @@ body { width: 4px; height: 4px; border-radius: 50%; - background: #2f4ee9; + background: #9df7ff; } .sidebar-nav { margin: 0; diff --git a/index.html b/index.html index e94bb7b..b689f8e 100644 --- a/index.html +++ b/index.html @@ -331,6 +331,106 @@

+
+
+ + + +

+ + + + + Comparator vs Comparable + + + +

+ + + +
+ +
+ + + + + + +

Comparable 和 Comparator 是JAVA 核心API 提供的两个接口。从它们的名字我们可以得知它们是用来比较东西的在某种意义上说。但他们到底是什么,它们之间的区别是什么呢?以下是回答这个问题的2个例子。比较两个HDTV的尺寸简单的例子。在阅读代码后,如何使用Comparable vs. Comparator就比较容易了。

+

1.Comparable

一个类实现Comparable接口,以便能够比较对象本身与一些其他对象。类本身必须实现此接口以便能够比较其实例(的)。所需实现的方法compareto()。这里就是一个例子:

+
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
class HDTV implements Comparable<HDTV> {
private int size;
private String brand;

public HDTV(int size, String brand) {
this.size = size;
this.brand = brand;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

@Override
public int compareTo(HDTV tv) {

if (this.getSize() > tv.getSize())
return 1;
else if (this.getSize() < tv.getSize())
return -1;
else
return 0;
}
}

public class Main {
public static void main(String[] args) {
HDTV tv1 = new HDTV(55, "Samsung");
HDTV tv2 = new HDTV(60, "Sony");

if (tv1.compareTo(tv2) > 0) {
System.out.println(tv1.getBrand() + " is better.");
} else {
System.out.println(tv2.getBrand() + " is better.");
}
}
}
+
+

Sony is better.

+
+

2.Comparator

在某些情况下,您可能不希望更改一个类,并使其具有可比较性。在这种情况下,如果你想比较基于某些属性/字段的对象,可以使用Comparator。例如,2个人可以基础上的身高或年龄等进行比较(Comparable不能被用于这种比较)。

+

需要实现的方法是compare()。现在让我们用另一种方式来比较这些电视的大小。比较器的一个常见用途是排序。集合和数组类提供了一种使用比较器的排序方法。

+
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class HDTV {
private int size;
private String brand;

public HDTV(int size, String brand) {
this.size = size;
this.brand = brand;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}
}

class SizeComparator implements Comparator<HDTV> {
@Override
public int compare(HDTV tv1, HDTV tv2) {
int tv1Size = tv1.getSize();
int tv2Size = tv2.getSize();

if (tv1Size > tv2Size) {
return 1;
} else if (tv1Size < tv2Size) {
return -1;
} else {
return 0;
}
}
}

public class Main {
public static void main(String[] args) {
HDTV tv1 = new HDTV(55, "Samsung");
HDTV tv2 = new HDTV(60, "Sony");
HDTV tv3 = new HDTV(42, "Panasonic");

ArrayList<HDTV> al = new ArrayList<HDTV>();
al.add(tv1);
al.add(tv2);
al.add(tv3);

Collections.sort(al, new SizeComparator());
for (HDTV a : al) {
System.out.println(a.getBrand());
}
}
}
+
+

Panasonic
Samsung
Sony

+
+

我们经常会使用Collections.reverseorder()方法得到一个降序比较器。如下:

+
1
2
3
4
5
6
7
8
9
10
11
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(3);
al.add(1);
al.add(2);
System.out.println(al);
Collections.sort(al);
System.out.println(al);

Comparator<Integer> comparator = Collections.reverseOrder();
Collections.sort(al,comparator);
System.out.println(al);
+

Output:

+
+

[3,1,2]
[1,2,3]
[3,2,1]

+
+

3.什么时候使用?

简单地说,一个类实现Comparable可以被比性,这意味着它的实例之间可以相互比较。
一个类实现Comparator将主要在两种情况下使用:1)它可以通过一个排序方法,如Collections.sort()或Arrays.sort(),允许精确控制的排序顺序和2)它也可以被用来控制的顺序一定的数据结构,如有序集合(例如TreeSet)或排序图(例如,Treemap)。

+

例如,创建一个TreeSet。我们可以通过构造一个Comparator或使对象类可比较。

+

方法1——TreeSet(Comparator comparator)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Dog {
int size;

Dog(int s) {
size = s;
}
}

class SizeComparator implements Comparator<Dog> {
@Override
public int compare(Dog d1, Dog d2) {
return d1.size - d2.size;
}
}

public class ImpComparable {
public static void main(String[] args) {
TreeSet<Dog> d = new TreeSet<Dog>(new SizeComparator()); // pass comparator
d.add(new Dog(1));
d.add(new Dog(2));
d.add(new Dog(1));
}
}

+

方法2——实现Comparable接口

+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Dog implements Comparable<Dog>{
int size;

Dog(int s) {
size = s;
}

@Override
public int compareTo(Dog o) {
return o.size - this.size;
}
}

public class ImpComparable {
public static void main(String[] args) {
TreeSet<Dog> d = new TreeSet<Dog>();
d.add(new Dog(1));
d.add(new Dog(2));
d.add(new Dog(1));
}
}
+

原文链接

+ + + +
+ + +
+ + + + +
@@ -1125,7 +1225,7 @@

@@ -1139,7 +1239,7 @@

diff --git a/tags/ClassCastException-Long-Integer/index.html b/tags/ClassCastException-Long-Integer/index.html index fa27584..cf5ade2 100644 --- a/tags/ClassCastException-Long-Integer/index.html +++ b/tags/ClassCastException-Long-Integer/index.html @@ -391,7 +391,7 @@

@@ -405,7 +405,7 @@

diff --git a/tags/Reading-List/index.html b/tags/Reading-List/index.html index 778ccdf..1a56d5b 100644 --- a/tags/Reading-List/index.html +++ b/tags/Reading-List/index.html @@ -391,7 +391,7 @@

@@ -405,7 +405,7 @@

diff --git a/tags/StringIndexOutOfBoundsException/index.html b/tags/StringIndexOutOfBoundsException/index.html index 56af13a..f7a5f8d 100644 --- a/tags/StringIndexOutOfBoundsException/index.html +++ b/tags/StringIndexOutOfBoundsException/index.html @@ -391,7 +391,7 @@

@@ -405,7 +405,7 @@

diff --git a/tags/comparator/index.html b/tags/comparator/index.html index a88a2b8..6fb39a0 100644 --- a/tags/comparator/index.html +++ b/tags/comparator/index.html @@ -59,7 +59,7 @@ - 标签: comparator // Courage Me + 标签: Comparator // Courage Me @@ -329,7 +329,7 @@

- comparator + Comparator 标签

@@ -342,14 +342,14 @@

- - 深入理解Arrays.sort()数组排序 + + Comparator vs Comparable

@@ -391,7 +391,7 @@

@@ -405,7 +405,7 @@

diff --git a/tags/first/index.html b/tags/first/index.html index b903f5c..f07c78c 100644 --- a/tags/first/index.html +++ b/tags/first/index.html @@ -391,7 +391,7 @@

@@ -405,7 +405,7 @@

diff --git a/tags/hello/index.html b/tags/hello/index.html index e40a749..ce8d34e 100644 --- a/tags/hello/index.html +++ b/tags/hello/index.html @@ -391,7 +391,7 @@

@@ -405,7 +405,7 @@

diff --git a/tags/index.html b/tags/index.html index 28856ea..24b597b 100644 --- a/tags/index.html +++ b/tags/index.html @@ -339,10 +339,10 @@

@@ -379,7 +379,7 @@

@@ -393,7 +393,7 @@

diff --git a/tags/sort/index.html b/tags/sort/index.html index a16e2f1..5682880 100644 --- a/tags/sort/index.html +++ b/tags/sort/index.html @@ -391,7 +391,7 @@

@@ -405,7 +405,7 @@

diff --git a/tags/video/index.html b/tags/video/index.html index 9e4b21a..24937fc 100644 --- a/tags/video/index.html +++ b/tags/video/index.html @@ -391,7 +391,7 @@

@@ -405,7 +405,7 @@

diff --git "a/tags/\346\236\204\351\200\240\345\207\275\346\225\260/index.html" "b/tags/\346\236\204\351\200\240\345\207\275\346\225\260/index.html" index 5c3cd2f..b9d3093 100644 --- "a/tags/\346\236\204\351\200\240\345\207\275\346\225\260/index.html" +++ "b/tags/\346\236\204\351\200\240\345\207\275\346\225\260/index.html" @@ -391,7 +391,7 @@