update:demo10

This commit is contained in:
dichgrem
2026-01-13 21:24:59 +08:00
parent 6922192da4
commit 6ce5c76843
3 changed files with 77 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
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));
}
}