Stream流(工厂的流水线)

文章目录

一、Stream流(工厂的流水线)

1、Stream流的介绍

Stream是 Java8中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复杂的查找、过滤和映射数据等操作。

Stream是数据渠道,用于操作数据源(集合、数组等)所生成的元素序列。“集合讲的是数据,流讲的是计算!”

注意:
    1.Stream自己不会存储元素。
    2.Stream不会改变源对象。相反,他们会返回一个持有结果的新Stream。
    3.Stream操作是延迟执行的。这意味着他们会等到需要结果的时候才执行。

2、Stream步骤:

  1. 创建Stream 通过数据源(集合、数组)创建Stream流
  2. 调用Stream的中间方法,处理数据
  3. 调用Stream的终结方法,接收处理后的数据

二、创建Stream流

第一种方式:通过Collection系列集合提供的Stream() 或 parallelStream()

public class StreamAPI01 {
    @Test
    public void test(){
        //1.通过Collection系列集合提供的Stream() 或 parallelStream()
        List<String> list = new ArrayList<>();
        Stream<String> stream01 = list.stream();
    }
}

第二种方式:通过Arrays 中的静态方法 stream() 获取数组流

public class StreamAPI01 {
    @Test
    public void test(){
        //2.通过Arrays 中的静态方法 stream() 获取数组流
        Employee[] emps = new Employee[10];
        Stream<Employee> stream02 = Arrays.stream(emps);
    }
}

第三种方式:通过Stream类中的静态方法 of()

public class StreamAPI01 {
    @Test
    public void test(){
        //3.通过Stream类中的静态方法 of()
        Stream<String> stream03 = Stream.of("aa", "bb", "cc");
    }
}

第四种方式:创建无限流

public class StreamAPI01 {
    @Test
    public void test(){
        //4.创建无限流
        //迭代
        Stream<Integer> stream04 = Stream.iterate(0,(x)->x+2);
        stream04.limit(10).forEach(System.out::println);

        //生成
        Stream.generate(()-> Math.random()).limit(5).forEach(System.out::println);
    }
}

三、中间操作

0、准备操作-创建 Employee类

public class Employee {
    private String name;
    private Integer age;
    private Double salary;

    public Employee() {
    }
    public Employee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Objects.equals(name, employee.name) && Objects.equals(age, employee.age) && Objects.equals(salary, employee.salary);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary);
    }
    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }
}

1、筛选与切片

filter 接收Lambda ,从流中排除某些元素。
limit 截断流,使其元素不超过给定数量。
skip(n) 跳过元素,返回一个扔掉了前n 个元素的流。若流中元素不足n 个,则返回一个空流。与limit(n)互补
distinct 筛选,通过流所生成元素的hashCode()和equals()去除重复元素

filter 接收Lambda ,从流中排除某些元素。

public class StreamAPI02 {
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 58, 5555.55),
            new Employee("王五", 26, 3333.33),
            new Employee("赵六", 36, 6666.66),
            new Employee("田七", 12, 8888.88),
            new Employee("田七", 12, 8888.88)
    );

    /**
     * filter 接收Lambda ,从流中排除某些元素。
     */
    @Test
    public void test01() {
        //中间操作:过滤出年龄大于32的员工
        Stream<Employee> stream = employees.stream()
                .filter((e) -> {
                    return e.getAge() > 32;
                });

        //终止操作:一次执行全部内容,即"惰性求值"
        List<Employee> collect = stream.collect(Collectors.toList()); //接收过滤后的最终结果
        collect.forEach(System.out::println);
    }
}
limit 截断流,使其元素不超过给定数量。(短路操作)
public class StreamAPI02 {
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 58, 5555.55),
            new Employee("王五", 26, 3333.33),
            new Employee("赵六", 36, 6666.66),
            new Employee("田七", 12, 8888.88),
            new Employee("田七", 12, 8888.88)
    );
    @Test
    public void test02() {
        //中间操作:获取工资大于5000的前三位员工
        Stream<Employee> stream = employees.stream().filter((e) -> {
            return e.getSalary() > 5000;
        }).limit(3);

        //终止操作
        List<Employee> collect = stream.collect(Collectors.toList());
        collect.forEach(System.out::println);
    }
}

skip(n) 跳过元素,返回一个扔掉了前n 个元素的流。若流中元素不足n 个,则返回一个空流。

public class StreamAPI02 {
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 58, 5555.55),
            new Employee("王五", 26, 3333.33),
            new Employee("赵六", 36, 6666.66),
            new Employee("田七", 12, 8888.88),
            new Employee("田七", 12, 8888.88)
    );
    @Test
    public void test03() {
        //中间操作:跳过前两个符合条件的数据
        Stream<Employee> stream = employees.stream().filter((e) -> {
            return e.getSalary() > 5000;
        }).skip(2);

        //终止操作
        List<Employee> collect = stream.collect(Collectors.toList());
        collect.forEach(System.out::println);
    }
}

distinct 筛选,通过流所生成元素的hashCode()和equals()去除重复元素

public class StreamAPI02 {
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 58, 5555.55),
            new Employee("王五", 26, 3333.33),
            new Employee("赵六", 36, 6666.66),
            new Employee("田七", 12, 8888.88),
            new Employee("田七", 12, 8888.88)
    );
    @Test
    public void test04() {
        //中间操作:跳过前两个符合条件的数据
        Stream<Employee> stream = employees.stream().filter((e) -> {
            return e.getSalary() > 5000;
        }).distinct();

        //终止操作
        List<Employee> collect = stream.collect(Collectors.toList());
        collect.forEach(System.out::println);
    }
}

2、映射

map 接收Lambda ,将元素转换成其他形式或提取信息。接收一个函数作为参数,该函数会被分发到每个元素上,并将其映射成一个新的元素。
flatMap 接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流
map 接收Lambda ,将元素转换成其他形式或提取信息。接收一个函数作为参数,该函数会被分发到每个元素上,并将其映射成一个新的元素。
public class StreamAPI02 {
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 58, 5555.55),
            new Employee("王五", 26, 3333.33),
            new Employee("赵六", 36, 6666.66),
            new Employee("田七", 12, 8888.88),
            new Employee("田七", 12, 8888.88)
    );
    @Test
    public void test1(){
        Stream<String> str = employees.stream().map((e) -> e.getName());
        System.out.println("-------------------------------------------");

        //map() 将函数应用到每一个元素上
        //将每一个字符串变成大写字符串
        List<String> strList = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee");
        Stream<String> stream = strList.stream().map(String::toUpperCase);
        stream.forEach(System.out::println);

        //将每一个字符串元素,拆成字符输出
        Stream<Stream<Character>> stream2 = strList.stream().map(StreamAPI03::filterCharacter);
        stream2.forEach((sm) -> {
            sm.forEach(System.out::println);
        });
    }
    //接收一个字符串,将其转成字符,并添加到list集合中,最后转成流返回
    public static Stream<Character> filterCharacter(String str){
        List<Character> list = new ArrayList<>();

        for (Character ch : str.toCharArray()) {
            list.add(ch);
        }

        return list.stream();
    }
}
flatMap 接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流
public class StreamAPI02 {
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 58, 5555.55),
            new Employee("王五", 26, 3333.33),
            new Employee("赵六", 36, 6666.66),
            new Employee("田七", 12, 8888.88),
            new Employee("田七", 12, 8888.88)
    );
    @Test
    public void test2(){
        //flatMap 合并流
        List<String> strList = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee");
        Stream<Character> stream3 = strList.stream().flatMap(StreamAPI03::filterCharacter);
        stream3.forEach(System.out::println);
    }
    //接收一个字符串,将其转成字符,并添加到list集合中,最后转成流返回
    public static Stream<Character> filterCharacter(String str){
        List<Character> list = new ArrayList<>();

        for (Character ch : str.toCharArray()) {
            list.add(ch);
        }

        return list.stream();
    }

}

3、排序

sorted() 自然排序
sorted(Comparator com) 定制排序

sorted() 自然排序

public class StreamAPI02 {
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 58, 5555.55),
            new Employee("王五", 26, 3333.33),
            new Employee("赵六", 36, 6666.66),
            new Employee("田七", 12, 8888.88),
            new Employee("田七", 12, 8888.88)
    );
    @Test
    public void test1(){
        List<String> list = Arrays.asList("dd", "ss", "aa","ab","bb","cd");

        //排序
        list.stream().sorted().forEach(System.out::println);
    }
}
sorted(Comparator com) 定制排序
public class StreamAPI02 {
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 58, 5555.55),
            new Employee("王五", 26, 3333.33),
            new Employee("赵六", 36, 6666.66),
            new Employee("田七", 12, 8888.88),
            new Employee("田七", 12, 8888.88)
    );
    @Test
    public void test2(){
        //升序,先按年龄排序,年龄相等按名字排序
        employees.stream().sorted((e1,e2)->{
            if (e1.getAge().equals(e2.getAge())){
                return e1.getName().compareTo(e2.getName());
            }else{
                return -e1.getAge().compareTo(e2.getAge());
            }
        }).forEach(System.out::println);
    }
}

4、查找与匹配

allMatch——检查是否匹配所有元素
anyMatch——检查是否至少匹配一个元素
noneMatch——检查是否没有匹配的元素
findFirst——返回第一个元素
findAny——返回当前流中的任意元素
count——返回流中元素的总个数
max——返回流中最大值
min——返回流中最小值

创建新的员工类

public class Employee02 {
    private int id;
    private String name;
    private int age;
    private double salary;
    private Status status;

    public Employee02() {
    }
    public Employee02(int id, String name, int age, double salary, Status status) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.status = status;
    }

    public Status getStatus() {
        return status;
    }
    public void setStatus(Status status) {
        this.status = status;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String show() {
        return "测试方法引用!";
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee02 that = (Employee02) o;
        return id == that.id && age == that.age && Double.compare(that.salary, salary) == 0 && Objects.equals(name, that.name) && status == that.status;
    }
    @Override
    public int hashCode() {
        return Objects.hash(id, name, age, salary, status);
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", status=" + status
                + "]";
    }
    public enum Status {
        FREE, BUSY, VOCATION;
    }
}

allMatch——检查是否匹配所有元素
anyMatch——检查是否至少匹配一个元素
noneMatch——检查是否没有匹配的元素

public class StreamAPI05 {
    List<Employee02> emps = Arrays.asList(
            new Employee02(102, "李四", 59, 6666.66, Status.BUSY),
            new Employee02(101, "张三", 18, 9999.99, Status.FREE),
            new Employee02(103, "王五", 28, 3333.33, Status.VOCATION),
            new Employee02(104, "赵六", 8, 7777.77, Status.BUSY),
            new Employee02(104, "赵六", 8, 7777.77, Status.FREE),
            new Employee02(104, "赵六", 8, 7777.77, Status.FREE),
            new Employee02(105, "田七", 38, 5555.55, Status.BUSY)
    );
    @Test
    public void test1(){
        //Status.BUSY 是否匹配全部元素
        boolean bl = emps.stream().allMatch((e) -> e.getStatus().equals(Status.BUSY));
        System.out.println(bl);

        //anyMatch() 是否匹配任意一个元素
        boolean bl1 = emps.stream().anyMatch((e) -> e.getStatus().equals(Status.BUSY));
        System.out.println(bl1);

        //anyMatch 检查有没有匹配的元素
        boolean bl2 = emps.stream().noneMatch((e) -> e.getStatus().equals(Status.BUSY));
        System.out.println(bl2);
    }
}

findFirst——返回第一个元素
findAny——返回当前流中的任意元素

public class StreamAPI05 {
    List<Employee02> emps = Arrays.asList(
            new Employee02(102, "李四", 59, 6666.66, Status.BUSY),
            new Employee02(101, "张三", 18, 9999.99, Status.FREE),
            new Employee02(103, "王五", 28, 3333.33, Status.VOCATION),
            new Employee02(104, "赵六", 8, 7777.77, Status.BUSY),
            new Employee02(104, "赵六", 8, 7777.77, Status.FREE),
            new Employee02(104, "赵六", 8, 7777.77, Status.FREE),
            new Employee02(105, "田七", 38, 5555.55, Status.BUSY)
    );
    @Test
    public void test2(){
        //返回排序后的第一个元素
        Optional<Employee02> op = emps.stream().sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())).findFirst();
        System.out.println(op.get());
        System.out.println("--------------------------------");
        
        //返回当前流中 等于Status.FREE 的任意元素
        Optional<Employee02> op2 = emps.parallelStream()
                .filter((e) -> e.getStatus().equals(Status.FREE))
                .findAny();

        System.out.println(op2.get());
    }
}

count——返回流中元素的总个数
max——返回流中最大值
min——返回流中最小值

public class StreamAPI05 {
    List<Employee02> emps = Arrays.asList(
            new Employee02(102, "李四", 59, 6666.66, Status.BUSY),
            new Employee02(101, "张三", 18, 9999.99, Status.FREE),
            new Employee02(103, "王五", 28, 3333.33, Status.VOCATION),
            new Employee02(104, "赵六", 8, 7777.77, Status.BUSY),
            new Employee02(104, "赵六", 8, 7777.77, Status.FREE),
            new Employee02(104, "赵六", 8, 7777.77, Status.FREE),
            new Employee02(105, "田七", 38, 5555.55, Status.BUSY)
    );
    @Test
    public void test3(){
        //返回元素等于Status.FREE,的总个数
        long count = emps.stream().filter((e) -> e.getStatus().equals(Status.FREE)).count();
        System.out.println(count);

        //返回当前流的最大值
        Optional<Double> op = emps.stream()
                .map(Employee02::getSalary)
                .max(Double::compare);
        System.out.println(op.get());

        //返回当前流的最小值
        Optional<Employee02> op2 = emps.stream()
                .min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(op2.get());
    }
}

5、归约与收集

reduce(T identity, BinaryOperator) / reduce(BinaryOperator) ——可以将流中元素反复结合起来,得到一个值。
collect——将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法

reduce(T identity, BinaryOperator) / reduce(BinaryOperator) ——可以将流中元素反复结合起来,得到一个值。

public class StreamAPI05 {
    List<Employee02> emps = Arrays.asList(
            new Employee02(102, "李四", 59, 6666.66, Status.BUSY),
            new Employee02(101, "张三", 18, 9999.99, Status.FREE),
            new Employee02(103, "王五", 28, 3333.33, Status.VOCATION),
            new Employee02(104, "赵六", 8, 7777.77, Status.BUSY),
            new Employee02(104, "赵六", 8, 7777.77, Status.FREE),
            new Employee02(104, "赵六", 8, 7777.77, Status.FREE),
            new Employee02(105, "田七", 38, 5555.55, Status.BUSY)
    );
    public void test1(){
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);

        //计算集合的元素的总和
        Integer sum = list.stream().reduce(0, (x, y) -> x + y);
        System.out.println(sum);
        System.out.println("----------------------------------------");

        //计算工资的总和
        Optional<Double> op = emps.stream().map(Employee02::getSalary).reduce(Double::sum);
        System.out.println(op.get());
    }
}

collect——将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法

public class StreamAPI05 {
    List<Employee02> emps = Arrays.asList(
            new Employee02(102, "李四", 59, 6666.66, Status.BUSY),
            new Employee02(101, "张三", 18, 9999.99, Status.FREE),
            new Employee02(103, "王五", 28, 3333.33, Status.VOCATION),
            new Employee02(104, "赵六", 8, 7777.77, Status.BUSY),
            new Employee02(104, "赵六", 8, 7777.77, Status.FREE),
            new Employee02(104, "赵六", 8, 7777.77, Status.FREE),
            new Employee02(105, "田七", 38, 5555.55, Status.BUSY)
    );
    @Test
    public void test2(){
        //获取emps集合中的全部名字,并存到List中
        List<String> list = emps.stream().map(Employee02::getName).collect(Collectors.toList());
        list.forEach(System.out::println);
        System.out.println("----------------------------------");

        //获取emps集合中的全部名字,并存到Set中
        Set<String> set = emps.stream().map(Employee02::getName).collect(Collectors.toSet());
        set.forEach(System.out::println);
        System.out.println("----------------------------------");

        //获取emps集合中的全部名字,并存到HashSet中
        HashSet<String> hs = emps.stream().map(Employee02::getName).collect(Collectors.toCollection(HashSet::new));
        hs.forEach(System.out::println);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值