Java中的List是一种常见的数据结构,它可以存储多个元素,并且允许重复元素存在。在Java中对List进行排序通常使用sort方法。
一、基本使用
sort方法是List接口中的一个方法,用于对列表中的元素进行排序。下面是对一个List进行简单排序的示例:
List<String> strList = new ArrayList<String>();
strList.add("apple");
strList.add("banana");
strList.add("pear");
Collections.sort(strList);
System.out.println(strList);
运行结果:
[apple, banana, pear]
排序方法是由Collections工具类提供的静态方法sort()实现的,它会自动按照字符串的字典序进行排序。
二、使用Comparator接口自定义排序规则
sort方法还可以通过实现Comparator接口来自定义排序规则。Comparator接口中有一个compare()方法,它用于比较两个元素的大小。
下面是一个自定义排序规则的示例,对Student对象按照成绩进行排序:
class Student {
private String name;
private int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("小明", 80));
studentList.add(new Student("小红", 90));
studentList.add(new Student("小王", 70));
Collections.sort(studentList, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s1.getScore() - s2.getScore();
}
});
for(Student s : studentList) {
System.out.println(s.getName() + " : " + s.getScore());
}
运行结果:
小王 : 70 小明 : 80 小红 : 90
这里通过实现Comparator这个接口,并实现compare()方法对List进行排序。在compare()方法中,如果s1比s2小,则返回负数;如果s1比s2大,则返回正数;如果s1和s2相等,则返回0。
三、使用Lambda表达式简化代码
在Java 8之后,可以使用Lambda表达式来替代Comparator接口中的compare()方法,代码变得更加简化。
下面是使用Lambda表达式对上一个示例进行优化:
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("小明", 80));
studentList.add(new Student("小红", 90));
studentList.add(new Student("小王", 70));
Collections.sort(studentList, (s1, s2) -> s1.getScore() - s2.getScore());
for(Student s : studentList) {
System.out.println(s.getName() + " : " + s.getScore());
}
运行结果:
小王 : 70 小明 : 80 小红 : 90
使用Lambda表达式可以省去定义Comparator接口的过程,代码更加简洁。
四、总结
sort方法是Java中对List进行排序的常用方法,它可以自动按照元素的大小进行排序,也可以通过实现Comparator接口来自定义排序规则。在Java 8中还可以使用Lambda表达式来简化代码。
代码示例:
List<String> strList = new ArrayList<String>();
strList.add("apple");
strList.add("banana");
strList.add("pear");
Collections.sort(strList);
System.out.println(strList);
class Student {
private String name;
private int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("小明", 80));
studentList.add(new Student("小红", 90));
studentList.add(new Student("小王", 70));
Collections.sort(studentList, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s1.getScore() - s2.getScore();
}
});
for(Student s : studentList) {
System.out.println(s.getName() + " : " + s.getScore());
}
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("小明", 80));
studentList.add(new Student("小红", 90));
studentList.add(new Student("小王", 70));
Collections.sort(studentList, (s1, s2) -> s1.getScore() - s2.getScore());
for(Student s : studentList) {
System.out.println(s.getName() + " : " + s.getScore());
}
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/187764.html
微信扫一扫
支付宝扫一扫