mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-05 07:31:57 -05:00
38 lines
954 B
Java
38 lines
954 B
Java
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));
|
||
}
|
||
}
|