Files
Java/com/study/demo10/LambdaCollection.java
2026-01-13 21:24:59 +08:00

38 lines
954 B
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.study.demo10;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Student {
String name;
int score;
Student(String name, int score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student{name='" + name + "', score=" + score + "}";
}
}
public class LambdaCollection {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("张三", 85));
students.add(new Student("李四", 92));
students.add(new Student("王五", 78));
System.out.println("排序前:");
students.forEach(s -> System.out.println(s));
Collections.sort(students, (s1, s2) -> s2.score - s1.score);
System.out.println("\n排序后按成绩降序");
students.forEach(s -> System.out.println(s));
}
}