Java中集合自定义排序(Comparator接口)

数组

从最简单的数组排序说起。

对于一个int数组,可以使用Arrays.sort(int[]),进行排序。排序规则默认是递增的。而现在要让它降序排列该怎么办呢?

有两个方法。

  • Collections.reverseOrder()
  • 重写Comparator接口的compare方法 下面着重讲述重写Comparator的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
import java.util.Arrays;
import java.util.Comparator;

public class MyArrays{

static Comparator comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {

return o2 - o1;
}
};

public static void main(String args[]){
Integer[] menber = {5,4,7,6,2,4,6,4,2};
Arrays.sort(menber, comparator);
for(Integer val : menber) {
System.out.print( val + " ");
}
}
}
//out:
/**
7 6 6 5 4 4 4 2 2
*/

通过实验看到,在compare方法中,o1-o2进行的是升序排列,而o2-o1是降序排列。为什么会这样呢?

不妨看看源码。

Arrays.sort()

1
2
3
4
5
6
7
8
9
10
public static <T> void sort(T[] a, Comparator<? super T> c) {
if (c == null) {
sort(a);
} else {
if (LegacyMergeSort.userRequested)
legacyMergeSort(a, c);
else
TimSort.sort(a, 0, a.length, c, null, 0, 0);
}
}

现在我们重写了compare方法,所以Comparetor是存在的,看else分支的代码。
legacyMergeSort()

1
2
3
4
5
6
7
8
/** To be removed in a future release. */
private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
T[] aux = a.clone();
if (c==null)
mergeSort(aux, a, 0, a.length, 0);
else
mergeSort(aux, a, 0, a.length, 0, c);
}

跳转到else分支

查看mergeSort(aux, a, 0, a.length, 0, c)代码:

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
private static void mergeSort(Object[] src,
Object[] dest,
int low, int high, int off,
Comparator c) {
int length = high - low;

// Insertion sort on smallest arrays
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
swap(dest, j, j-1);
return;
}

// Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) >>> 1;
mergeSort(dest, src, low, mid, -off, c);
mergeSort(dest, src, mid, high, -off, c);

// If list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if (c.compare(src[mid-1], src[mid]) <= 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
}

// Merge sorted halves (now in src) into dest
for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}

重点看这段代码

1
2
3
4
5
6
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
swap(dest, j, j-1);
return;
}

满足这个条件时(c.compare(dest[j-1], dest[j])>0、j>low),才能进行swap操作,也就是交换数组元素。

当返回值大于0,也就是dest[j-1]>dest[j],在compare方法中就是o1大于o2,即o1-o2>0。就将dest[j-1]、dest[j]交换顺序,从而得到升序的排序序列。

同理o2 - o1大于0,就不交换顺序,也就是大得数在前面,小的数在后面。

示例:

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
import java.util.*;

public class Test{

static Person[] person = {
new Person(3,"a"),
new Person(1,"b"),
new Person(4,"c"),
new Person(7,"d")
};

static Comparator<Person> comparator = new Comparator<Person>() {

@Override
public int compare(Person o1, Person o2) {
return o1.name.compareTo(o2.name);
}

};
public static void main(String args[]){
Arrays.sort(person,comparator);
for(Person val : person) {
System.out.println( val.toString() + " ");
}
}
}

class Person {
Integer id;
String name;

@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
public Person(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
}

ArrayList

注意:假如要对基本类型进行排序,要使用它们对应得包装类型。(int———Integer)

Collections.sort(list, comparator)。

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
import java.util.*;

public class Test{

static Person[] person = {
new Person(3,"a"),
new Person(1,"b"),
new Person(4,"c"),
new Person(7,"d")
};

static Comparator<Person> comparator = new Comparator<Person>() {

@Override
public int compare(Person o1, Person o2) {
return o1.name.compareTo(o2.name);
}

};
public static void main(String args[]){
List<Person> list = new ArrayList<Person>();

list.add(new Person(4,"c"));
list.add(new Person(3,"a"));
list.add(new Person(1,"b"));
list.add(new Person(7,"d"));
Collections.sort(list, comparator);
for(Person val : list) {
System.out.println( val.toString() + " ");
}
}
}

class Person {
Integer id;
String name;

@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
public Person(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
}

PriorityQueue

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
import java.util.*;

public class Test{

static Person[] person = {
new Person(3,"a"),
new Person(1,"b"),
new Person(4,"c"),
new Person(7,"d")
};

static Comparator<Person> comparator = new Comparator<Person>() {

@Override
public int compare(Person o1, Person o2) {
return o2.name.compareTo(o1.name);
}

};
public static void main(String args[]){
Queue<Person> queue = new PriorityQueue<Person>(comparator);
queue.add(new Person(3,"a"));
queue.add(new Person(1,"b"));
queue.add(new Person(4,"c"));
queue.add(new Person(7,"d"));
for(Person val : queue) {
System.out.println( val.toString() + " ");
}
}
}

class Person {
Integer id;
String name;

@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
public Person(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
}

TreeSet

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
import java.util.*;

public class Test{

static Person[] person = {
new Person(3,"a"),
new Person(1,"b"),
new Person(4,"c"),
new Person(7,"d")
};

static Comparator<Person> comparator = new Comparator<Person>() {

@Override
public int compare(Person o1, Person o2) {
return o1.name.compareTo(o2.name);
}

};
public static void main(String args[]){
Set<Person> list = new TreeSet<Person>(comparator);

list.add(new Person(4,"c"));
list.add(new Person(3,"a"));
list.add(new Person(1,"b"));
list.add(new Person(7,"d"));
// Collections.sort(list, comparator);
for(Person val : list) {
System.out.println( val.toString() + " ");
}
}
}

class Person {
Integer id;
String name;

@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
public Person(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
}
-------------本文结束感谢您的阅读-------------
0%