java-----集合的常规操作(流操作)

这篇博客探讨了Java集合框架中Stream API的使用,展示了如何通过流操作对集合进行过滤、映射和收集等高效处理。Stream API使得代码更加简洁,提高了可读性和灵活性。例如,通过filter和collect方法筛选并获取特定年份的学生列表,或者使用map方法转换为只包含名字的列表。此外,还介绍了如何使用groupingBy方法按性别分组学生,以及利用removeIf方法删除满足条件的元素。Stream API的这些特性简化了集合处理,提升了代码质量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接下来用一个例子来展示一下集合的应用以及流的操作主函数自行编写哈;
下面这个类是一个接口的实现类,里面对泛类为Students的List执行了各种操作,但是很多操作都先将集合转型成了Stream也就是流,然后根据流的各种操作而操作,这样有什么样的好处呢?
流就像一条生产线上的物件,将集合转成流就像将其放在生产线上,一条生产线上可以先后执行许多操作最终得到想要的结果,就像下面这一段代码,一条生产线上可以有这么多个操作于是就节省了许多代码,也提升了代码的简洁性;

return DatabaseUtils.getStudents().stream()
              .filter(s->s.getYear()==year)
              .collect(Collectors.toList());

所以将集合转型成流操作就有这样的好处,而且这样操作得到的“产品”也可以多样化:

 return DatabaseUtils.getStudents().stream()
              .filter(s->s.getYear()==year&&s.getSex()==sex)
              .map(s->s.getName())
              .collect(Collectors.toList());

以上代码就生成了与List不一样的集合,也就是产生了不一样的“产品”,这也是流的好处。
除了以上以外下面代码还有有关集合的其他操作,客官也就自行汲取自己想要看的东西吧。

package com.experiment04.Service.impl;

import com.experiment04.Service.StudentService;
import com.experiment04.entity.Student;
import com.experiment04.resource.DatabaseUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StudentServiceImpl implements StudentService {
    @Override
    public   List<Student> addStudent(Student student) {
        DatabaseUtils.getStudents().add(student);
        return DatabaseUtils.getStudents() ;
    }

    @Override
    public List<Student> listStudentsByYear(int year) {
      return DatabaseUtils.getStudents().stream()
              .filter(s->s.getYear()==year)
              .collect(Collectors.toList());
    }

    @Override
    public List<String> listStudentsNames(int year, Student.Sex sex) {


        return DatabaseUtils.getStudents().stream()
              .filter(s->s.getYear()==year&&s.getSex()==sex)
              .map(s->s.getName())
              .collect(Collectors.toList());
    }

    @Override
    public Map<Student.Sex, List<Student>> mapStudentsBySex() {
        return  DatabaseUtils.getStudents().stream()
                .collect(Collectors.groupingBy(Student::getSex));

    }

    @Override
    public boolean removeStudent(int id) {
         return DatabaseUtils.getStudents().removeIf(s->s.getId()==id);

    }
}

接下来几个是上面用到的封装好的几个类和接口
学生类:包含了ID 性别 姓名等几个成员变量,以及一个带参构造方法,和几个get、set方法;

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

DatabaseUtils类 :创建一个初始的学生集合并用get方法传送:

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接口类:里面声明了各种对学生集合的操作的抽象方法由最开始的StudentServiceImpl类实现:

package com.experiment04.Service.impl;

import com.experiment04.Service.StudentService;
import com.experiment04.entity.Student;
import com.experiment04.resource.DatabaseUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StudentServiceImpl implements StudentService {
    @Override
    public   List<Student> addStudent(Student student) {
        DatabaseUtils.getStudents().add(student);
        return DatabaseUtils.getStudents() ;
    }

    @Override
    public List<Student> listStudentsByYear(int year) {
      return DatabaseUtils.getStudents().stream()
              .filter(s->s.getYear()==year)
              .collect(Collectors.toList());
    }

    @Override
    public List<String> listStudentsNames(int year, Student.Sex sex) {


        return DatabaseUtils.getStudents().stream()
              .filter(s->s.getYear()==year&&s.getSex()==sex)
              .map(s->s.getName())
              .collect(Collectors.toList());
    }

    @Override
    public Map<Student.Sex, List<Student>> mapStudentsBySex() {
        return  DatabaseUtils.getStudents().stream()
                .collect(Collectors.groupingBy(Student::getSex));

    }

    @Override
    public boolean removeStudent(int id) {
         return DatabaseUtils.getStudents().removeIf(s->s.getId()==id);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值