集合与函数式编程实验
在com.experiment04.entity下,创建Student类,代码如下;
在com.experiment04.resource下,创建DatabaseUtils类,代码如下;
在com.experiment04.service下,创建StudentService接口,代码如下;
在com.experiment04.service.impl下,创建StudentService接口的实现类StudentServiceImpl,基于集合stream等操作,按接口方法注释需求实现
代码如下:
main
1 | import com.experiment04.entity.Student; |
StudentServiceImpl
bash
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
我多加了个初始值方便测
public void util() {
///qingkong
studentList.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));
}
@Override
public List<Student> addStudent(Student student) {
studentList.add(student);
return studentList;
}
@Override
public 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;
}
@Override
public 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;
}
@Override
public 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;
}
@Override
public 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);
}
}
1 | #### DatabaseUtils |
StudentService
bash
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);
}
1 | #### Student |