亲宝软件园·资讯

展开

java Stream API Java8新特性之Stream API详解

油炸冰可乐 人气:0
想了解Java8新特性之Stream API详解的相关内容吗,油炸冰可乐在本文为您仔细讲解java Stream API的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,Stream,API,Java8,Stream详解,下面大家一起来学习吧。

一、前言

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

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

二、使用流程

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

三、案例演示

public class EmployeeData {
   
   public static List<Employee> getEmployees(){
      List<Employee> 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<Employee> list = EmployeeData.getEmployees();
        // 返回一个顺序流
        Stream<Employee> stream = list.stream();
        // 返回一个并行流
        Stream<Employee> 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<Employee> stream = Arrays.stream(list2);
        stream.forEach(System.out::println);

    }

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

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

    // 使用StreamAPI筛选
    @Test
    public void test5() {
        List<Employee> 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<String> list = Arrays.asList("a", "b", "c");
//        map(Function<? super T,? extends R> mapper)
//        返回由给定函数应用于此流的元素的结果组成的流。
        list.stream().map(s -> s.toUpperCase()).forEach(System.out::println);

    }

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

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

    }

    // StreamAPI排序,sorted帮助实现Comparable接口进行对象比较
    @Test
    public void test9() {
        List<Employee> 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<Employee> list = EmployeeData.getEmployees();
        boolean allMatch = list.stream().allMatch(e -> e.getSalary() > 5000);
        System.out.println(allMatch);
    }

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

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

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

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

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

加载全部内容

相关教程
猜你喜欢
用户评论