以下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方法
}
在以下類中,按需求撰寫方法,完成對STUDENTS集合的操作
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) {
// 呼叫實作方法測驗
}
// 實作方法
}
說明:
需求描述中的指定X,均指方法的引數
所有方法均有回傳值,嘗試直接編程return陳述句,基于stream操作流直接回傳所需結果
如果回傳集合,使用List集合型別
嘗試使用簡寫
注意過濾代碼格式
方法1,獲取成績小于等于指定分數,的全部學生
答案:
方法2,獲取指定班級,成績小于等于指定分數,的全部學生
答案:
方法3,獲取指定班級,成績小于等于指定分數,的全部學生的姓名。注意回傳型別
答案:
方法4,按成績由低到高排序,回傳全部學生
答案:
方法5,獲取指定班級,成績小于等于指定分數,成績由高到低排序,的全部學生
答案:
方法6,獲取指定班級,成績小于等于指定分數,成績由高到低排序,的全部學生的學號。注意回傳型別
答案:
方法7,獲取指定班級,成績小于等于指定分數,的全部學生
以學生學號為鍵,學生分數為值,Map分組,回傳
答案:
uj5u.com熱心網友回復:
不就是操作api嗎,這個自己去實作吧
uj5u.com熱心網友回復:
public static void main(String[] args) {
//方法1,獲取成績小于等于指定分數,的全部學生
System.out.println("-------------------------------");
List<Student> scoredayu = STUDENTS.stream().filter(student -> student.getScore() > 75).collect(Collectors.toList());
scoredayu.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法2,獲取指定班級,成績小于等于指定分數,的全部學生
List<Student> collect2 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).collect(Collectors.toList());
collect2.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法3,獲取指定班級,成績小于等于指定分數,的全部學生的姓名。注意回傳型別
List<String> collect3 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).map(student -> student.getName()).collect(Collectors.toList());
collect3.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法4,按成績由低到高排序,回傳全部學生
List<Student> collect4 = STUDENTS.stream().sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getScore() - o2.getScore();
}
}).collect(Collectors.toList());
collect4.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法5,獲取指定班級,成績小于等于指定分數,成績由高到低排序,的全部學生
List<Student> collect5 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getScore() - o2.getScore();
}
}).collect(Collectors.toList());
collect5.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法6,獲取指定班級,成績小于等于指定分數,成績由高到低排序,的全部學生的學號。注意回傳型別
List<Integer> collect6 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getScore() - o1.getScore();
}
}).map(student -> student.getNumber()).collect(Collectors.toList());
collect6.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法7,獲取指定班級,成績小于等于指定分數,的全部學生
List<Student> collect7 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).collect(Collectors.toList());
collect7.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
}
還是要謝謝題主,本來這些知識學得迷迷糊糊,看到之后不知道怎么的忽然就跟開竅了一樣,這樣以后作業中我遍歷就list做篩選就比較方便了
uj5u.com熱心網友回復:
參考 2 樓 qq_41622282的回復:
public static void main(String[] args) {
//方法1,獲取成績小于等于指定分數,的全部學生
System.out.println("-------------------------------");
List<Student> scoredayu = STUDENTS.stream().filter(student -> student.getScore() > 75).collect(Collectors.toList());
scoredayu.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法2,獲取指定班級,成績小于等于指定分數,的全部學生
List<Student> collect2 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).collect(Collectors.toList());
collect2.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法3,獲取指定班級,成績小于等于指定分數,的全部學生的姓名。注意回傳型別
List<String> collect3 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).map(student -> student.getName()).collect(Collectors.toList());
collect3.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法4,按成績由低到高排序,回傳全部學生
List<Student> collect4 = STUDENTS.stream().sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getScore() - o2.getScore();
}
}).collect(Collectors.toList());
collect4.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法5,獲取指定班級,成績小于等于指定分數,成績由高到低排序,的全部學生
List<Student> collect5 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getScore() - o2.getScore();
}
}).collect(Collectors.toList());
collect5.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法6,獲取指定班級,成績小于等于指定分數,成績由高到低排序,的全部學生的學號。注意回傳型別
List<Integer> collect6 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getScore() - o1.getScore();
}
}).map(student -> student.getNumber()).collect(Collectors.toList());
collect6.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法7,獲取指定班級,成績小于等于指定分數,的全部學生
List<Student> collect7 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).collect(Collectors.toList());
collect7.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
}
還是要謝謝題主,本來這些知識學得迷迷糊糊,看到之后不知道怎么的忽然就跟開竅了一樣,這樣以后作業中我遍歷就list做篩選就比較方便了
謝謝你的幫助。
uj5u.com熱心網友回復:
參考 2 樓 qq_41622282的回復:
public static void main(String[] args) {
//方法1,獲取成績小于等于指定分數,的全部學生
System.out.println("-------------------------------");
List<Student> scoredayu = STUDENTS.stream().filter(student -> student.getScore() > 75).collect(Collectors.toList());
scoredayu.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法2,獲取指定班級,成績小于等于指定分數,的全部學生
List<Student> collect2 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).collect(Collectors.toList());
collect2.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法3,獲取指定班級,成績小于等于指定分數,的全部學生的姓名。注意回傳型別
List<String> collect3 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).map(student -> student.getName()).collect(Collectors.toList());
collect3.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法4,按成績由低到高排序,回傳全部學生
List<Student> collect4 = STUDENTS.stream().sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getScore() - o2.getScore();
}
}).collect(Collectors.toList());
collect4.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法5,獲取指定班級,成績小于等于指定分數,成績由高到低排序,的全部學生
List<Student> collect5 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getScore() - o2.getScore();
}
}).collect(Collectors.toList());
collect5.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法6,獲取指定班級,成績小于等于指定分數,成績由高到低排序,的全部學生的學號。注意回傳型別
List<Integer> collect6 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getScore() - o1.getScore();
}
}).map(student -> student.getNumber()).collect(Collectors.toList());
collect6.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
//方法7,獲取指定班級,成績小于等于指定分數,的全部學生
List<Student> collect7 = STUDENTS.stream().filter(student -> CLAZZ2.equals(student.getClazz()) && student.getScore() < 75).collect(Collectors.toList());
collect7.stream().forEach(student -> System.out.println(student));
System.out.println("-------------------------------");
}
還是要謝謝題主,本來這些知識學得迷迷糊糊,看到之后不知道怎么的忽然就跟開竅了一樣,這樣以后作業中我遍歷就list做篩選就比較方便了
謝謝你的幫助。
uj5u.com熱心網友回復:
第一個題寫錯了 寫成了大于75的學生 因為這個我沒有做限定,都是以75為成績標準的 這個你可以看情況改一下
uj5u.com熱心網友回復:
參考 1 樓 純技術男人的回復: 不就是操作api嗎,這個自己去實作吧
我是小白,需要大家幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/71410.html
標籤:Java相關
上一篇:js寫音樂播放器初始化時間顯示為nan,點擊下一首時出現的時間是上一首的,時間對不上或者直接顯示nan,能不能給個解決方法,急用!
下一篇:求解,有沒有人遇到過這樣的問題