java实验四 集合与函数式编程实验

(2020.4.28版)
desktop/javacode/javatesthomework4


5.12版 改了StudentServiceImpl 为stream方式
5.19 优化removeif


一、实验目的:

  1. 掌握集合的基本操作方法
  2. 掌握Lambda表达式的声明编写规范
  3. 掌握基于集合Stream的过滤/映射/聚合等操作
  4. 掌握基于函数式编程集合元素的移除方法
    二、实验内容:
    初始化
    理解实验的目的,不要浪费时间在初始化的代码,直接复制使用

在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));
        /1show
        studentServiceShow.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() {
        ///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);
   
    }
}



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 get
    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;

    }

}
  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值