欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java8新特性之Stream API详解

程序员文章站 2022-03-03 11:05:23
一、前言streamapi在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);
        }
    }
}

到此这篇关于java8新特性之streamapi详解的文章就介绍到这了,更多相关java streamapi详解内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: java Stream API