本文目录导读:

我来通过几个经典案例详细介绍Java中实现排序的多种方式。
基础数组排序
使用Arrays.sort()
import java.util.Arrays;
public class BasicSortDemo {
public static void main(String[] args) {
// 基本类型数组排序(快速排序)
int[] numbers = {64, 34, 25, 12, 22, 11, 90};
Arrays.sort(numbers);
System.out.println("基本类型排序: " + Arrays.toString(numbers));
// 字符串数组排序(按字典序)
String[] fruits = {"banana", "apple", "orange", "grape"};
Arrays.sort(fruits);
System.out.println("字符串排序: " + Arrays.toString(fruits));
// 部分排序
int[] partial = {3, 1, 4, 1, 5, 9, 2, 6};
Arrays.sort(partial, 2, 6); // 只排序索引2到5的元素
System.out.println("部分排序: " + Arrays.toString(partial));
}
}
集合排序
使用Collections.sort()
import java.util.*;
public class CollectionSortDemo {
public static void main(String[] args) {
// List排序
List<Integer> numbers = new ArrayList<>(Arrays.asList(64, 34, 25, 12, 22, 11, 90));
Collections.sort(numbers);
System.out.println("List排序: " + numbers);
// 字符串List排序
List<String> fruits = new ArrayList<>(Arrays.asList("banana", "apple", "orange"));
Collections.sort(fruits);
System.out.println("字符串List排序: " + fruits);
// 反向排序
Collections.sort(numbers, Collections.reverseOrder());
System.out.println("反向排序: " + numbers);
}
}
自定义对象排序
Comparable接口实现
import java.util.*;
// 实现Comparable接口
class Student implements Comparable<Student> {
private String name;
private int score;
private int age;
public Student(String name, int score, int age) {
this.name = name;
this.score = score;
this.age = age;
}
// 按分数排序(自然排序)
@Override
public int compareTo(Student other) {
// 升序:this.score - other.score
// 降序:other.score - this.score
return this.score - other.score;
}
@Override
public String toString() {
return String.format("Student{name='%s', score=%d, age=%d}", name, score, age);
}
public String getName() { return name; }
public int getScore() { return score; }
public int getAge() { return age; }
}
public class ObjectSortDemo {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("张三", 85, 20));
students.add(new Student("李四", 92, 19));
students.add(new Student("王五", 78, 21));
students.add(new Student("赵六", 92, 18));
// 使用Comparable排序(按分数)
Collections.sort(students);
System.out.println("按分数排序:");
students.forEach(System.out::println);
}
}
Comparator接口实现(多种排序方式)
import java.util.*;
public class ComparatorSortDemo {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("张三", 85, 20));
students.add(new Student("李四", 92, 19));
students.add(new Student("王五", 78, 21));
students.add(new Student("赵六", 92, 18));
// 1. 按年龄排序
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getAge() - s2.getAge();
}
});
System.out.println("按年龄排序:");
students.forEach(System.out::println);
// 2. Lambda表达式(Java 8+)
Collections.sort(students, (s1, s2) -> s1.getName().compareTo(s2.getName()));
System.out.println("按姓名排序:");
students.forEach(System.out::println);
// 3. 多条件排序:先按分数降序,再按年龄升序
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// 先比较分数(降序)
int scoreCompare = s2.getScore() - s1.getScore();
if (scoreCompare != 0) {
return scoreCompare;
}
// 分数相同,按年龄升序
return s1.getAge() - s2.getAge();
}
});
System.out.println("按分数降序、年龄升序排序:");
students.forEach(System.out::println);
}
}
Stream API排序(Java 8+)
import java.util.*;
import java.util.stream.Collectors;
public class StreamSortDemo {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("张三", 85, 20));
students.add(new Student("李四", 92, 19));
students.add(new Student("王五", 78, 21));
students.add(new Student("赵六", 92, 18));
// 1. 自然排序
List<Student> sortedByScore = students.stream()
.sorted()
.collect(Collectors.toList());
// 2. 按年龄排序
List<Student> sortedByAge = students.stream()
.sorted(Comparator.comparingInt(Student::getAge))
.collect(Collectors.toList());
// 3. 按分数降序
List<Student> sortedByScoreDesc = students.stream()
.sorted(Comparator.comparingInt(Student::getScore).reversed())
.collect(Collectors.toList());
// 4. 多条件排序
List<Student> multiSorted = students.stream()
.sorted(Comparator
.comparingInt(Student::getScore).reversed()
.thenComparingInt(Student::getAge))
.collect(Collectors.toList());
System.out.println("Stream API排序结果:");
multiSorted.forEach(System.out::println);
// 5. 数组也可以使用Stream
int[] numbers = {64, 34, 25, 12, 22, 11, 90};
int[] sorted = Arrays.stream(numbers)
.sorted()
.toArray();
System.out.println("Stream排序数组: " + Arrays.toString(sorted));
}
}
实用案例:学生成绩管理系统
import java.util.*;
import java.util.stream.Collectors;
public class ScoreManagementSystem {
private List<Student> students = new ArrayList<>();
public void addStudent(Student student) {
students.add(student);
}
// 多种排序方式
public void displayRankings() {
System.out.println("\n=== 成绩排行榜 ===");
System.out.println("1. 按成绩排序");
System.out.println("2. 按年龄排序");
System.out.println("3. 按姓名排序");
System.out.println("4. 综合排序(成绩降序,年龄升序)");
System.out.print("请选择排序方式: ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
List<Student> sortedStudents;
switch (choice) {
case 1:
sortedStudents = students.stream()
.sorted(Comparator.comparingInt(Student::getScore).reversed())
.collect(Collectors.toList());
break;
case 2:
sortedStudents = students.stream()
.sorted(Comparator.comparingInt(Student::getAge))
.collect(Collectors.toList());
break;
case 3:
sortedStudents = students.stream()
.sorted(Comparator.comparing(Student::getName))
.collect(Collectors.toList());
break;
case 4:
sortedStudents = students.stream()
.sorted(Comparator
.comparingInt(Student::getScore).reversed()
.thenComparingInt(Student::getAge))
.collect(Collectors.toList());
break;
default:
sortedStudents = new ArrayList<>(students);
}
System.out.println("排名\t姓名\t成绩\t年龄");
int rank = 1;
for (Student s : sortedStudents) {
System.out.printf("%d\t%s\t%d\t%d\n",
rank++, s.getName(), s.getScore(), s.getAge());
}
}
public static void main(String[] args) {
ScoreManagementSystem system = new ScoreManagementSystem();
system.addStudent(new Student("张三", 85, 20));
system.addStudent(new Student("李四", 92, 19));
system.addStudent(new Student("王五", 78, 21));
system.addStudent(new Student("赵六", 92, 18));
system.addStudent(new Student("陈七", 85, 22));
system.displayRankings();
}
}
性能考虑与建议
import java.util.*;
public class SortPerformanceTips {
public static void main(String[] args) {
// 1. 基本类型用Arrays.sort()(快速排序)
int[] primitiveArray = {5, 2, 8, 1, 9};
Arrays.sort(primitiveArray);
// 2. 对象数组用Arrays.sort()(归并排序的变体)
Integer[] objectArray = {5, 2, 8, 1, 9};
Arrays.sort(objectArray);
// 3. 大集合使用并行排序
List<Integer> largeList = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 1000000; i++) {
largeList.add(random.nextInt(1000000));
}
// 并行排序(适合大数据量)
long start = System.currentTimeMillis();
largeList.parallelStream().sorted().collect(Collectors.toList());
long end = System.currentTimeMillis();
System.out.println("并行排序耗时: " + (end - start) + "ms");
// 4. 避免在循环中排序
// 错误示例:
// for (...) {
// Collections.sort(list); // 每次循环都排序
// }
// 正确做法:循环外排序一次
// Collections.sort(list);
// for (...) { ... }
}
}
| 排序方式 | 使用场景 | 特点 |
|---|---|---|
| Arrays.sort() | 数组排序 | 快速排序,高效 |
| Collections.sort() | 集合排序 | 归并排序,稳定 |
| Comparable接口 | 自然排序 | 定义默认排序规则 |
| Comparator接口 | 自定义排序 | 灵活的多种排序方式 |
| Stream API | 函数式编程 | 代码简洁,支持并行 |
| 并行排序 | 大数据量 | 利用多核提升性能 |
选择合适的排序方式可以显著提升代码的可读性和性能,对于大多数场景,使用Java内置的排序方法就足够了。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播