【笔记】Java的排序

前言

Java的排序学习笔记
正序表示从小到大排序,倒序表示从大到小排序
排序方法返回值小于等于0表示正序,返回值大于0表示倒序

对基本数据类型数据排序

使用Arrays工具类对数组排序

  • 正序

  • Arrays.sort()会直接对原数组进行排序,不会创建新的数组

1
2
3
int[] array = new int[];
Arrays.sort(array);
System.out.println(Arrays.toString(array));

使用Stream对数组排序

使用内置比较器

  • 正序
1
2
3
4
5
int[] array = new int[];
int[] result = IntStream.of(array)
.sorted()
.toArray();
System.out.println(Arrays.toString(array));

对实现了Comparable接口的任意类型数据排序

包装类

  • 包装类默认实现了Compareable接口,可以直接使用

使用Arrays工具类对数组排序

  • 正序
1
2
Integer[] array = new Integer[];
Arrays.sort(array);
  • 倒序
1
2
Integer[] array = new Integer[];
Arrays.sort(array, Comparator.reverseOrder());

使用Stream对数组排序

使用内置比较器

Comparator.naturalOrder():缺省值,正序
Comparator.reverseOrder():倒序
Comparator.comparing(<field>):按指定字段正序
Comparator.comparing(<field>).reversed():按指定字段倒序
Comparator.comparingInt(<field>)Comparator.comparingLong(<field>)Comparator.comparingDouble(<field>):按指定字段正序排序,且基本数据类型不会装箱
Comparator.nullsFirst(<field>):按指定字段null值放在最前
Comparator.nullsLast(<field>):按指定字段null值放在最后

1
2
3
4
5
Integer[] array = new Integer[];
Integer[] result = Arrays.stream(array)
.sorted(Comparator.naturalOrder())
.toArray(Integer[]::new);
System.out.println(Arrays.toString(array));

使用Collections工具类对List集合排序

使用内置比较器
  • 正序排序,省略了比较器
1
2
List<Integer> list = new ArrayList<>();
Collections.sort(list);
  • 正序排序,没有省略比较器
1
2
3
Comparator<Student> comparator = Collections.naturalOrder();
List<Integer> list = new ArrayList<>();
Collections.sort(list, comparator);
  • 按指定字段正序排序
1
2
3
Comparator<Student> comparator = Collections.comparing(Integer::value);
List<Integer> list = new ArrayList<>();
Collections.sort(list, comparator);
  • 通过reversed()反转正序,实现按指定字段倒序排序
1
2
3
Comparator<Student> comparator = Collections.comparing(Integer::value).reversed();
List<Integer> list = new ArrayList<>();
Collections.sort(list, comparator);
  • 多字段、正序倒序组合排序

thenComparingInt:上一个排序结果的顺序相同时才会执行下一个排序方式

1
2
3
4
5
Comparator<Student> comparator = Collections.comparing(Integer::value).reversed()
.thenComparingInt(Integer::value)
.thenComparingInt(Integer::value);
List<Integer> list = new ArrayList<>();
Collections.sort(list, comparator);

使用List接口的默认方法对List集合排序

  • 默认方法(default method)是Java8引入的新特性,允许接口中的方法被default修饰,被default修饰的方法可以实现方法体
  • Java8中的List包含默认方法sort(),本质上就是Collections.sort()
    • Java8以前只能使用Collections.sort()进行排序
    • Java8及以后推荐使用List.sort()进行排序
使用内置比较器
1
2
List<Integer> list = new ArrayList<>();
list.sort(Comparator.naturalOrder());

使用Stream对List集合排序

使用内置比较器
1
2
3
4
List<Integer> list = new ArrayList<>();
List<Integer> result = list.stream()
.sorted(Comparator.naturalOrder())
.collect(Collectors.toList());

自定义类

使用Arrays工具类对数组排序

  • 正序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Cls implements Comparable<Cls> {

int field;

public Cls(int field) {
this.field = field;
}

@Override
public int compareTo(Cls o) {
return this.field - o.field;
}
}

public class Main {
public static void main(String[] args) {
Cls[] array = new Cls[];
Arrays.sort(array);
}
}
  • 倒序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Cls implements Comparable<Cls> {

int field;

public Cls(int field) {
this.field = field;
}

@Override
public int compareTo(Cls o) {
return this.field - o.field;
}
}

public class Main {
public static void main(String[] args) {
Cls[] array = new Cls[];
Arrays.sort(array, Comparator.reverseOrder());
}
}

使用Stream对数组排序

使用内置比较器

传送门

使用自定义比较器
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
class Cls implements Comparable<Cls> {

int field;

public Cls(int field) {
this.field = field;
}

@Override
public int compareTo(Cls o) {
return this.field - o.field;
}
}

class ClsComparator implements Comparator<Cls> {
@Override
public int compare(Cls o1, Cls o2) {
return this.field - o.field;
}
}

public class Main {
public static void main(String[] args) {
Cls[] array = new Cls[];
Cls[] result = Arrays.stream(array)
.sorted(Comparator.naturalOrder())
.toArray(Cls[]::new);
System.out.println(Arrays.toString(array));
}
}
使用Lambda表达式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Cls implements Comparable<Cls> {

int field;

public Cls(int field) {
this.field = field;
}

@Override
public int compareTo(Cls o) {
return this.field - o.field;
}
}

public class Main {
public static void main(String[] args) {
Cls[] array = new Cls[];
Cls[] result = Arrays.stream(array)
.sorted((o1, o2) -> o1.field - o2.field)
.toArray(Cls[]::new);
System.out.println(Arrays.toString(array));
}
}

使用Collections工具类对List集合排序

使用内置比较器

传送门

使用自定义比较器

传送门

使用List接口的默认方法对List集合排序

使用内置比较器

传送门

使用自定义比较器

传送门

使用Stream对List集合排序

使用内置比较器

传送门

使用自定义比较器

传送门

使用Stream对Set集合排序

使用内置比较器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.ArrayList;

class Cls implements Comparable<Cls> {

int field;

public Cls(int field) {
this.field = field;
}

@Override
public int compareTo(Cls o) {
return this.field - o.field;
}
}

public class Main {
public static void main(String[] args) {
Set<Cls> set = new LinkedHashSet<>();
LinkedHashSet<Cls> result = set.stream()
.sorted(Comparator.naturalOrder())
.collect(Collectors.toCollection(LinkedHashSet::new));
}
}
使用自定义比较器

传送门

使用Stream对Map集合排序

使用内置比较器
根据Key排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.ArrayList;

class Cls implements Comparable<Cls> {

int field;

public Cls(int field) {
this.field = field;
}

@Override
public int compareTo(Cls o) {
return this.field - o.field;
}
}

public class Main {
public static void main(String[] args) {
Map<Cls, Cls> map = new LinkedHashMap<>();
Map<Cls, Cls> result = map.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
}
根据Value排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.ArrayList;

class Cls implements Comparable<Cls> {

int field;

public Cls(int field) {
this.field = field;
}

@Override
public int compareTo(Cls o) {
return this.field - o.field;
}
}

public class Main {
public static void main(String[] args) {
Map<Cls, Cls> map = new LinkedHashMap<>();
Map<Cls, Cls> result = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
}
使用自定义比较器

传送门

完成