JAVA集合流练习
以下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分组,返回
直接上源代码了,按顺序写了七个的实现方法,输出就不写了
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
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;
}
public int getNumber(){
return number;
}
public void setNumber(){
this.number=number;
}
public String getName(){
return name;
}
public void setName(){
this.name=name;
}
public String getClazz(){
return clazz;
}
public void setClazz(){
this.clazz=clazz;
}
public int getScore(){
return score;
}
public void setScore(){
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 List<Student> getStudents() {
return STUDENTS;
}
public static void main(String[] args) {
// 调用实现方法测试
}
//获取成绩小于等于指定分数,的全部学生
public List<Student> getStudents1(int score){
return getStudents().stream()
.filter(s->s.getScore()==score)
.collect(Collectors.toList());
}
//获取指定班级,成绩小于等于指定分数,的全部学生
public List<Student> getStudents2(String CLAZZ,int score){
return getStudents().stream()
.filter(u->u.getClazz().equals(CLAZZ))
.filter(s->s.getScore()<=score)
.collect(Collectors.toList());
}
//获取指定班级,成绩小于等于指定分数,的全部学生的姓名。注意返回类型
public List<String> getStudents3(String CLAZZ,int score){
return getStudents().stream()
.filter(u->u.getClazz().equals(CLAZZ))
.filter(s->s.getScore()<=score)
.map(Student::getName)
.collect(Collectors.toList());
}
//按成绩由低到高排序,返回全部学生
public List<Student> getStudents4(){
return getStudents().stream().sorted
(Comparator.comparing(Student::getScore))
.collect(Collectors.toList());
}
//获取指定班级,成绩小于等于指定分数,成绩由高到低排序,的全部学生
public List<Student> getStudents5(int score,String clazz){
return getStudents().stream()
.filter(s->s.getClazz().equals(clazz))
.filter(u->u.getScore()<=score)
.sorted(Comparator.comparing(Student::getScore).reversed())
.collect(Collectors.toList());
}
//获取指定班级,成绩小于等于指定分数,成绩由高到低排序,的全部学生的学号。注意返回类型
public List<Integer> getStudents6(int score,String clazz){
return getStudents().stream()
.filter(s->s.getClazz().equals(clazz))
.filter(u->u.getScore()<=score)
.sorted(Comparator.comparing(Student::getScore).reversed())
.map(Student::getNumber)
.collect(Collectors.toList());
}
//方法7,获取指定班级,成绩小于等于指定分数,的全部学生以学生学号为键,学生分数为值,Map分组,返回
public Map<Integer,Integer> getStudents7(int score,String clazz){
return getStudents().stream()
.filter(s->s.getClazz().equals(clazz))
.filter(u->u.getScore()<=score)
.collect(Collectors.toMap(Student::getNumber,Student::getScore));
}
// 实现方法
}