java实验四 集合与函数式编程实验
生活随笔
收集整理的這篇文章主要介紹了
java实验四 集合与函数式编程实验
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
(2020.4.28版)
desktop/javacode/javatesthomework4
5.12版 改了StudentServiceImpl 為stream方式
5.19 優化removeif
nefu2020.4.28版,剛剛交
- main
- StudentServiceImpl
- DatabaseUtils
- StudentService
- Student
一、實驗目的:
二、實驗內容:
初始化
理解實驗的目的,不要浪費時間在初始化的代碼,直接復制使用
在com.experiment04.entity下,創建Student類,直接從github復制使用實驗代碼
在com.experiment04.resource下,創建DatabaseUtils類,直接從github復制使用實驗代碼,模擬添加若干對象
在com.experiment04.service下,創建StudentService接口,直接從github復制使用實驗代碼
在com.experiment04.service.impl下,創建StudentService接口的實現類StudentServiceImpl,基于集合stream等操作,按接口方法注釋需求實現
在com.experiment04下,創建Test類,
為每個接口方法創建靜態測試方法,在main()主函數調用
面向接口編程,測試StudentService接口中方法的實現
main
import com.experiment04.entity.Student; import com.experiment04.service.StudentService; import com.experiment04.service.impl.StudentServiceImpl;public class main {public static void main(String[] args) {StudentService studentService=new StudentServiceImpl();///方便看StudentServiceImpl studentServiceShow= (StudentServiceImpl) studentService;1.加入System.out.println("--------------1.向集合添加一個學生,返回當前全部學生----------------------");studentService.addStudent(new Student(201001, Student.Sex.FEMALE, "趙陽陽", 2010));studentService.addStudent(new Student(201002, Student.Sex.MALE, "邵鵬", 2010));studentService.addStudent(new Student(201103, Student.Sex.MALE, "高學斌", 2011));studentService.addStudent(new Student(201104, Student.Sex.MALE, "張揚", 2011));studentService.addStudent(new Student(201205, Student.Sex.FEMALE, "呂惠玲", 2012));studentService.addStudent(new Student(201206, Student.Sex.MALE, "曾志優", 2012));/1showstudentServiceShow.studentList.forEach(u-> System.out.println(u.getId()+" "+u.getName()+" "+u.getYear()+" "+u.getSex()+" "));2.返回指定屆的全部學生System.out.println("---------------2.返回指定屆的全部學生---------------------");studentService.listStudentsByYear(2010).forEach(u-> System.out.println(u.getId()+" "+u.getName()+" "+u.getYear()+" "+u.getSex()+" "));///3.返回指定屆,指定性別的全部學生的姓名System.out.println("----------------3.返回指定屆,指定性別的全部學生的姓名--------------------");studentService.listStudentsNames(2010, Student.Sex.MALE).forEach(u-> System.out.println(u));///4.將所有學生,以性別分組System.out.println("----------------4.將所有學生,以性別分組--------------------");try {studentService.mapStudentsBySex().get(Student.Sex.MALE).forEach(u-> System.out.println(u.getId()+" "+u.getName()+" "+u.getYear()+" "+u.getSex()+" "));studentService.mapStudentsBySex().get(Student.Sex.FEMALE).forEach(u-> System.out.println(u.getId()+" "+u.getName()+" "+u.getYear()+" "+u.getSex()+" "));} catch (Exception e) {///e.printStackTrace();System.out.println("有個空了");}///5.刪除指定學號的學生,返回是否成功刪除System.out.println("----------------5.刪除指定學號的學生,返回是否成功刪除--------------------");System.out.println(studentService.removeStudent(201002));((StudentServiceImpl) studentService).studentList.forEach(u-> System.out.println(u.getId()+" "+u.getName()+" "+u.getYear()+" "+u.getSex()+" "));} }StudentServiceImpl
package com.experiment04.service.impl;import com.experiment04.entity.Student; import com.experiment04.resource.DatabaseUtils; import com.experiment04.service.StudentService;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; import java.util.stream.Stream;public class StudentServiceImpl implements StudentService {public List<Student>studentList=new ArrayList<Student>();我多加了個初始值方便測public void util() {///qingkongstudentList.removeIf(u->{return true;});studentList.add(new Student(201001, Student.Sex.FEMALE, "趙陽陽", 2010));studentList.add(new Student(201002, Student.Sex.MALE, "邵鵬", 2010));studentList.add(new Student(201103, Student.Sex.MALE, "高學斌", 2011));studentList.add(new Student(201104, Student.Sex.MALE, "張揚", 2011));studentList.add(new Student(201205, Student.Sex.FEMALE, "呂惠玲", 2012));studentList.add(new Student(201206, Student.Sex.MALE, "曾志優", 2012));}@Overridepublic List<Student> addStudent(Student student) {studentList.add(student);return studentList;}@Overridepublic List<Student> listStudentsByYear(int year) {List<Student>studentsYear=new ArrayList<Student>();/*studentList.forEach(u->{if(u.getYear()==year){studentsYear.add(u);}});*/Stream<Student>stream =DatabaseUtils.getStudents().stream();Stream<Student>newstream= stream.filter(s->{if(s.getYear()==year){return true;}return false;});studentsYear=newstream.collect(Collectors.toList());return studentsYear;}@Overridepublic List<String> listStudentsNames(int year, Student.Sex sex) {List <String>yearSearchList=new ArrayList<String>();studentList.forEach(u->{if(u.getYear()==year && u.getSex()==sex){yearSearchList.add(u.getName());}});return yearSearchList;}@Overridepublic Map<Student.Sex, List<Student>> mapStudentsBySex() {Map<Student.Sex,List<Student>> map=new HashMap<>();map.put(Student.Sex.MALE,new ArrayList<Student>());map.put(Student.Sex.FEMALE,new ArrayList<Student>());studentList.forEach(u->{if(u.getSex()== Student.Sex.MALE){map.get(Student.Sex.MALE).add(u);}if(u.getSex()== Student.Sex.FEMALE){map.get(Student.Sex.FEMALE).add(u);}});return map;}@Overridepublic boolean removeStudent(int id) { //AtomicBoolean flag= new AtomicBoolean(false);/* studentList=studentList.stream().filter(i->{if(i.getId()!=id){return true;}return false;}).collect(Collectors.toList());return true;*//*return studentList.removeIf(u->{if(u.getId()==id){return true;}return false;});*//**/return DatabaseUtils.getStudents().removeIf(s->s.getId()==id);} }DatabaseUtils
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;}}StudentService
package com.experiment04.service; import com.experiment04.entity.Student; import com.experiment04.resource.DatabaseUtils;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);}Student
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;}下面全是set getpublic 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;}} 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java实验四 集合与函数式编程实验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WebGL简易教程(十四):阴影
- 下一篇: Qt的安装