2020/5/6
主要題目都註釋在主函數及下面的方法中
————————————————————————————————
Student 類
public class Student {
private int number;
private String name;
private String clazz;
private int score;
public Student(int number, String name, String clazz, int score) {
this.number = number;
this.name = name;
this.clazz = clazz;
this.score = score;
}
// 省略getter/setter方法
public int getScore() {
return score;
}
public int getNumber() {
return number;
}
public String getName() {
return name;
}
public String getClazz() {
return clazz;
}
}
StreamTest.java
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
private static final List <Student> STUDENTS = create();
private static final String CLAZZ1 = "軟件1班";
private static final String CLAZZ2 = "軟件2班";
private static List <Student> create() {
Student s1 = new Student(2018008, "張揚", CLAZZ2, 66);
Student s2 = new Student(2018005, "劉飛", CLAZZ1, 92);
Student s3 = new Student(2018007, "李明", CLAZZ2, 42);
Student s4 = new Student(2018006, "趙勇", CLAZZ2, 56);
Student s5 = new Student(2018002, "王磊", CLAZZ1, 81);
Student s6 = new Student(2018010, "牛娜", CLAZZ1, 78);
List<Student> students = new ArrayList<>();
students.add(s1);students.add(s2);students.add(s3);
students.add(s4);students.add(s5);students.add(s6);
return students;
}
public static void main(String[] args) {
// 調用實現方法測試
//f4(STUDENTS);
//f5(STUDENTS,95,"軟件1班");
// f6(STUDENTS,95,"軟件1班");
f7(STUDENTS,95,"軟件1班");
}
// 以下都是實現方法————
public static void f7(List <Student> s,int grade,String b){
//獲取指定班級,成績小於等於指定分數,的全部學生
//以學生學號爲鍵,學生分數爲值,Map分組,返回
s.stream()
.filter(a->a.getScore()<=grade&&b.equals(a.getClazz()))
.collect(Collectors.toMap(a->a.getNumber(),a->a.getScore()))
.forEach((k,v)->{
System.out.println(k+" "+v);
});
}
public static void f6(List <Student> s,int grade,String b){
//獲取指定班級,成績小於等於指定分數,成績由高到低排序,的全部學生的學號
s.stream()
.filter(a->a.getScore()<=grade&&b.equals(a.getClazz()))
.sorted(Comparator.comparing(Student::getScore).reversed())
.collect(Collectors.toList())
.forEach(a->{
System.out.println(a.getNumber());
});
}
public static void f5(List <Student> s,int grade,String b){
// 獲取指定班級,成績小於等於指定分數,成績由高到低排序,的全部學生
s.stream()
.filter(a->a.getScore()<=grade&&b.equals(a.getClazz()))
.sorted(Comparator.comparing(Student::getScore).reversed())
.collect(Collectors.toList())
.forEach(a->{
System.out.println(a.getNumber()+" "+a.getName()+" "+a.getClazz()+" "+a.getScore());
});
}
public static void f4(List <Student> s){
//方法4,按成績由低到高排序,返回全部學生
s.stream()
.sorted(Comparator.comparing(a->a.getScore()))
.collect(Collectors.toList())
.forEach(a->{
System.out.println(a.getNumber()+""+a.getName()+" "+a.getClazz()+" "+a.getScore());
});
}
public static List<Student> f2(List<Student> s,int grade,String b){
return s.stream()
.filter(a->a.getScore()<=grade&&b.equals(a.getClazz()))
.collect(Collectors.toList());
}
private static List<Student> fangfa1(){
// 獲取70分以下的人
List<Student> newlist = new ArrayList<>();
for(Student stu : STUDENTS) {
if(stu.getScore()<70)
newlist.add(stu);
}
return newlist;
}
}