Java8新特性之Stream API代码示例解析

作者:袖梨 2022-06-29

本篇文章小编给大家分享一下Java8新特性之Stream API代码示例解析,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

一、前言

StreamAPI在Java8版本中使用,关注的是对数据的筛选、查找、存储等

它可以做的事情有:过滤、排序、映射、归约

二、使用流程

Stream实例化中间操作(过滤、排序、映射、规约)终止操作(匹配查找、归约、收集)

三、案例演示

public class EmployeeData {
   
   public static List getEmployees(){
      List list = new ArrayList<>();
      
      list.add(new Employee(1001, "马化腾", 34, 6000.38));
      list.add(new Employee(1002, "马云", 12, 9876.12));
      list.add(new Employee(1003, "刘强东", 33, 3000.82));
      list.add(new Employee(1004, "雷军", 26, 7657.37));
      list.add(new Employee(1005, "李彦宏", 65, 5555.32));
      list.add(new Employee(1006, "比尔盖茨", 42, 9500.43));
      list.add(new Employee(1007, "任正非", 26, 4333.32));
      list.add(new Employee(1008, "扎克伯格", 35, 2500.32));
      
      return list;
   }
   
}
package JDK_8;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamAPI {
    // 方式1:使用集合创建Stream
    @Test
    public void test1() {
        List list = EmployeeData.getEmployees();
        // 返回一个顺序流
        Stream stream = list.stream();
        // 返回一个并行流
        Stream employeeStream = list.parallelStream();
    }

    // 方式2:使用数组创建流对象
    @Test
    public void test2() {
        Employee e1 = new Employee(1003, "WZY", 26, 3000.69);
        Employee e2 = new Employee(1007, "王紫玉", 25, 8888);

        Employee[] list2 = new Employee[]{e1, e2};
        Stream stream = Arrays.stream(list2);
        stream.forEach(System.out::println);

    }

    // 通过Stream.of 创建
    @Test
    public void test3() {
        Stream stream = Stream.of(1, 2, 3, 4, 5, 6);
        stream.forEach(System.out::println);
    }

    // Stream.filter进行过滤
    @Test
    public void test4() {
        List list = EmployeeData.getEmployees();
        list.stream().filter(employee -> employee.getAge() > 25).forEach(System.out::println);
    }

    // 使用StreamAPI筛选
    @Test
    public void test5() {
        List list = EmployeeData.getEmployees();
        list.add(new Employee(1004, "wzy", 25, 18888));
        list.add(new Employee(1004, "wzy", 25, 18888));
        list.add(new Employee(1004, "wzy", 25, 18888));
        list.add(new Employee(1004, "wzy", 25, 18888));

        list.stream().forEach(System.out::println);
        System.out.println();
        list.stream().distinct().forEach(System.out::println);
    }

    @Test
    public void test6() {
        List list = Arrays.asList("a", "b", "c");
//        map(Function mapper)
//        返回由给定函数应用于此流的元素的结果组成的流。
        list.stream().map(s -> s.toUpperCase()).forEach(System.out::println);

    }

    // 使用StreamAPI筛选:工资大于6000的员工
    @Test
    public void test7() {
        List list = EmployeeData.getEmployees();
        list.stream().filter(s -> s.getSalary() > 6000).forEach(System.out::println);
    }

    // StreamAPI映射:map,接收参数,将参数转换为其他形式的信息;
    @Test
    public void test8() {
        List list = EmployeeData.getEmployees();
        list.stream().map(e -> e.getAge()).forEach(System.out::println);

    }

    // StreamAPI排序,sorted帮助实现Comparable接口进行对象比较
    @Test
    public void test9() {
        List list = EmployeeData.getEmployees();
        list.stream().sorted((e1, e2) -> {
            int ageValue = Integer.compare(e1.getAge(), e2.getAge());
            if (ageValue != 0) {
                return ageValue;
            } else {
                return Double.compare(e1.getSalary(), e2.getSalary());
            }
        }).forEach(System.out::println);
    }

    // StreamAPI匹配:所有元素是否满足下列条件
    @Test
    public void test10() {
        List list = EmployeeData.getEmployees();
        boolean allMatch = list.stream().allMatch(e -> e.getSalary() > 5000);
        System.out.println(allMatch);
    }

    // StreamAPI查找:返回第一个元素
    @Test
    public void test11() {
        List list = EmployeeData.getEmployees();
        Optional first = list.stream().findFirst();
        System.out.println(first);
    }

    // StreamAPI查找:返回薪水数最大的员工
    @Test
    public void test12() {
        List list = EmployeeData.getEmployees();
        Stream salaryStream = list.stream().map(e -> e.getSalary());
        Optional max = salaryStream.max(Double::compare);
        System.out.println(max);
    }

    // StreamAPI查找:薪水最小的员工对像
    @Test
    public void test13() {
        List list = EmployeeData.getEmployees();
        Optional min = list.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(min);
    }

    // StreamAPI:归约
    @Test
    public void test14() {
        List list = EmployeeData.getEmployees();
        Stream salaryStream = list.stream().map(e -> e.getSalary());
        Optional sum = salaryStream.reduce((s1, s2) -> s1 + s2);
        System.out.println(sum.get());
    }

    // StreamAPI:收集
    @Test
    public void test15(){
        List list = EmployeeData.getEmployees();
        List employeeList = list.stream().filter(e -> e.getSalary() > 5000).collect(Collectors.toList());
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
    }
}

相关文章

精彩推荐