前言:代码是分开写的,大家用的时候写在一个包里面就行

题目内容:

在com.experiment04.entity下,创建Student类,代码如下;

在com.experiment04.resource下,创建DatabaseUtils类,代码如下;

在com.experiment04.service下,创建StudentService接口,代码如下;

在com.experiment04.service.impl下,创建StudentService接口的实现类StudentServiceImpl,
基于集合stream等操作,按接口方法注释需求实现

在com.experiment04下,创建Test类,

为每个接口方法创建静态测试方法,在main()主函数调用

面向接口编程,测试StudentService接口中方法的实现

com/experiment04/entity/Student.java
package com.experiment04.entity; public class Student { public enum Sex {
MALE, FEMALE } private int id; private Sex sex; private String name; // 例如2017级
private int year; public Student(int id, Sex sex, String name, int year) {
this.id = id; this.sex = sex; this.name = name; this.year = year; } public int
getId() { return id; } public void setId(int id) { this.id = id; } public Sex
getSex() { return sex; } public void setSex(Sex sex) { this.sex = sex; } public
String getName() { return name; } public void setName(String name) { this.name
= name; } public int getYear() { return year; } public void setYear(int year) {
this.year = year; } }
 com/experiment04/resource/DatabaseUtils.java
package com.experiment04.resource; import com.experiment04.entity.Student;
import java.util.ArrayList; import java.util.List; public class DatabaseUtils {
private static final List<Student> STUDENTS = create(); private static
List<Student> create() { List<Student> students = new ArrayList<>();
students.add(new Student(201001, Student.Sex.FEMALE, "赵阳阳", 2010));
students.add(new Student(201002, Student.Sex.MALE, "邵鹏", 2010));
students.add(new Student(201103, Student.Sex.MALE, "高学斌", 2011));
students.add(new Student(201104, Student.Sex.MALE, "张扬", 2011));
students.add(new Student(201205, Student.Sex.FEMALE, "吕惠玲", 2012));
students.add(new Student(201206, Student.Sex.MALE, "曾志优", 2012)); return
students; } public static List<Student> getStudents() { return STUDENTS; } }
 com/experiment04/service/impl/StudentServiceImpl.java
package com.experiment04.service.impl; import com.experiment04.entity.Student;
import com.experiment04.resource.DatabaseUtils; import
com.experiment04.service.StudentService; import java.util.Collections; import
java.util.List; import java.util.Map; import java.util.stream.Collectors;
public class StudentServiceImpl implements StudentService { public
List<Student> addStudent(Student student) { List<Student>
students=DatabaseUtils.getStudents(); students.add(student); return students; }
public List<Student> listStudentsByYear(int year) { List<Student>
students=DatabaseUtils.getStudents(); return students.stream() .filter(student
-> student.getYear()==year) .collect(Collectors.toList()); } public
List<String> listStudentsNames(int year, Student.Sex sex) { List<Student>
students=DatabaseUtils.getStudents(); return students.stream() .filter(student
-> student.getYear()==year&&student.getSex()==sex) .map(student ->
student.getName()) .collect(Collectors.toList()); } public Map<Student.Sex,
List<Student>> mapStudentsBySex() { List<Student>
students=DatabaseUtils.getStudents(); return students.stream()
.collect(Collectors.groupingBy(student -> student.getSex())); } public boolean
removeStudent(int id) { List<Student> students=DatabaseUtils.getStudents();
return students.removeIf(student -> student.getId()==id); } }
 com/experiment04/service/StudentService.java
package com.experiment04.service; import com.experiment04.entity.Student;
import java.util.List; import java.util.Map; public interface StudentService {
/** * 向集合添加一个学生,返回当前全部学生 * @param student * @return */ List<Student>
addStudent(Student student); /** * 返回指定届的全部学生 * @param year * @return */
List<Student> listStudentsByYear(int year); /** * 返回指定届,指定性别的全部学生的姓名 * @param
year * @param sex * @return */ List<String> listStudentsNames(int year,
Student.Sex sex); /** * 将所有学生,以性别分组 * @return */ Map<Student.Sex,
List<Student>> mapStudentsBySex(); /** * 删除指定学号的学生,返回是否成功删除 * @param id *
@return */ boolean removeStudent(int id); }
 com/experiment04/Test.java
package com.experiment04; import com.experiment04.entity.Student; import
com.experiment04.service.StudentService; import
com.experiment04.service.impl.StudentServiceImpl; public class Test { private
static StudentService stuSer=new StudentServiceImpl(); public static void
addStuTest() { Student s=new Student(1001, Student.Sex.FEMALE,"zhangsan",2016);
System.out.println("-------addStuTest---------"); stuSer.addStudent(s)
.forEach(a->{ System.out.println(a.getId()+" "+a.getName()+" "+a.getSex()+"
"+a.getYear()); }); } public static void listStudentsByYear(){ int year=2016;
System.out.println("-------listStudentsByYear---------");
stuSer.listStudentsByYear(year) .forEach(a->{ System.out.println(a.getId()+"
"+a.getName()+" "+a.getSex()+" "+a.getYear()); }); } public static void
listStudentsNames(){ int year=2012; Student.Sex sex= Student.Sex.FEMALE;
System.out.println("-------listStudentsNames---------");
System.out.println(stuSer.listStudentsNames(year,sex)); } public static void
removeStudent(){ int id=1001;
System.out.println("-------removeStudent---------");
System.out.println(stuSer.removeStudent(id)); } public static void
main(String[] args) { addStuTest(); listStudentsByYear(); listStudentsNames();
removeStudent(); } }

技术
©2019-2020 Toolsou All rights reserved,
Python学习笔记(一)Linux【shell】 shell编程创建一个线程——— Javaweb (3)evo工具使用问题——Degenerate covariance rank, Umeyama alignment is not possibleVMware 16安装centos 7详细教程C语言做一个简易的登陆验证(功能)界面C语言——qsort函数Spring Boot面试必问:自动配置原理Android EditText密码显示隐藏Qt入门教程【基础控件篇】QCalendarWidget日历控件