流(stream())是java8的一个新特性,主要的做用就是将各类类型的集合转换为流,而后的方便迭代数据用的.例如:html
//将List类型的集合转换为流 list.stream()
转换为流以后能够进行一系列的迭代操做,比本身去拿出list的值一个个操做要方便的多.java
使用流以前,必须先对函数式接口、lambda表达式和方法引用有一些了解,若是您不具有这方面知识,请移驾lambda表达式&方法引用.数组
使用的oracle默认的emp表的字段:oracle
public class Emp { private BigDecimal empno; private boolean trueOrFalse; private String ename; private String job; private BigDecimal mgr; private Date hiredate; private Double sal; private BigDecimal comm; private BigDecimal deptno; public BigDecimal getEmpno() { return empno; } public void setEmpno(BigDecimal empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename == null ? null : ename.trim(); } public String getJob() { return job; } public void setJob(String job) { this.job = job == null ? null : job.trim(); } public BigDecimal getMgr() { return mgr; } public void setMgr(BigDecimal mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public BigDecimal getComm() { return comm; } public void setComm(BigDecimal comm) { this.comm = comm; } public BigDecimal getDeptno() { return deptno; } public void setDeptno(BigDecimal deptno) { this.deptno = deptno; } }
获得工资在1000以上的员工的集合:app
//获得list集合 List<Emp> listEmp = empService.listEmp(); /* * 1. listEmp.stream() 将集合转换为流, * 这样就能够用流的方法对集合进行迭代. * * 2.filter方法.里面的emp至关于拿到集合中的每个emp进行操做, * 结果要返回一个Boolean值 * * 3. .collect(toList()),实际是.collect(Collectors.toList()). * */ List<Emp> result = listEmp.stream().filter(emp -> emp.getSal() > 1000).collect(toList()); System.out.println("result = " + result); 打印输出: result = [Emp(empno=7499, trueOrFalse=false, ename=ALLEN, job=SALESMAN, mgr=7698, hiredate=Fri Feb 20 00:00:00 CST 1981, sal=1600.0, comm=300, deptno=30), Emp(empno=7521, trueOrFalse=false, ename=WARD, job=SALESMAN, mgr=7698, hiredate=Sun Feb 22 00:00:00 CST 1981, sal=1250.0, comm=500, deptno=30), Emp(empno=7566, trueOrFalse=false, ename=JONES, job=MANAGER, mgr=7839, hiredate=Thu Apr 02 00:00:00 CST 1981, sal=2975.0, comm=null, deptno=20), Emp(empno=7654, trueOrFalse=false, ename=MARTIN, job=SALESMAN, mgr=7698, hiredate=Mon Sep 28 00:00:00 CST 1981, sal=1250.0, comm=1400, deptno=30), Emp(empno=7698, trueOrFalse=false, ename=BLAKE, job=MANAGER, mgr=7839, hiredate=Fri May 01 00:00:00 CST 1981, sal=2850.0, comm=null, deptno=30), Emp(empno=7782, trueOrFalse=false, ename=CLARK, job=MANAGER, mgr=7839, hiredate=Tue Jun 09 00:00:00 CST 1981, sal=2450.0, comm=null, deptno=10), Emp(empno=7788, trueOrFalse=false, ename=SCOTT, job=ANALYST, mgr=7566, hiredate=Sun Apr 19 00:00:00 CDT 1987, sal=3000.0, comm=null, deptno=20), Emp(empno=7839, trueOrFalse=false, ename=KING, job=PRESIDENT, mgr=null, hiredate=Tue Nov 17 00:00:00 CST 1981, sal=5000.0, comm=null, deptno=10), Emp(empno=7844, trueOrFalse=false, ename=TURNER, job=SALESMAN, mgr=7698, hiredate=Tue Sep 08 00:00:00 CST 1981, sal=1500.0, comm=0, deptno=30), Emp(empno=7876, trueOrFalse=false, ename=ADAMS, job=CLERK, mgr=7788, hiredate=Sat May 23 00:00:00 CDT 1987, sal=1100.0, comm=null, deptno=20), Emp(empno=7902, trueOrFalse=false, ename=FORD, job=ANALYST, mgr=7566, hiredate=Thu Dec 03 00:00:00 CST 1981, sal=3000.0, comm=null, deptno=20), Emp(empno=7934, trueOrFalse=false, ename=MILLER, job=CLERK, mgr=7782, hiredate=Sat Jan 23 00:00:00 CST 1982, sal=1300.0, comm=null, deptno=10)]
结果返回了全部工资在1000以上的员工.函数
分布介绍一下方法,.filter()会对集合中的每个元素都执行一次括号内的操做:性能
.filter(emp -> emp.getSal() > 1000)
咱们追一下filter方法,能够看到:this
Stream<T> filter(Predicate<? super T> predicate);
返回的是一个流,参数是一个Predicate.再追这个参数:lua
@FunctionalInterface public interface Predicate<T> { /** * Evaluates this predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */ boolean test(T t);
@FunctionalInterface注解说明这是一个函数式接口.并非有了这个注释才说明这是一个函数式接口,而是只有一个抽象方法的接口,就是函数式接口.code
接口的方法:
boolean test(T t);
参数是任意类型,返回值Boolean类型.
也就是说.filter()方法,能够传递任意类型的参数,返回值必须是Boolean类型的,只要知足这两个条件,均可以传到filter方法中.
咱们传递的,参数emp,返回值emp.getSal()>1000是个Boolean值,因此知足:
.filter(emp -> emp.getSal() > 1000)
.collect(toList())将结果转换为一个list集合,真正地写法为:
.collect(Collectors.toList());
由于我在这个类中使用了静态导入:
import static java.util.stream.Collectors.*;
因此前面的Collectors能够不写.
有时候,获得一个集合只须要其中的几位,这时候可使用截断:
List<Emp> result = listEmp.stream() .filter(emp -> emp.getSal() > 1000) //截取3位 .limit(3) .collect(toList()); System.out.println("result = " + result); 打印输出:result = [Emp(empno=7499, trueOrFalse=false, ename=ALLEN, job=SALESMAN, mgr=7698, hiredate=Fri Feb 20 00:00:00 CST 1981, sal=1600.0, comm=300, deptno=30), Emp(empno=7521, trueOrFalse=false, ename=WARD, job=SALESMAN, mgr=7698, hiredate=Sun Feb 22 00:00:00 CST 1981, sal=1250.0, comm=500, deptno=30), Emp(empno=7566, trueOrFalse=false, ename=JONES, job=MANAGER, mgr=7839, hiredate=Thu Apr 02 00:00:00 CST 1981, sal=2975.0, comm=null, deptno=20)]
.limit()方法,能够截取指定的位数
刚刚是截断3位,此次咱们输出跳过3位的结果,使用.skip():
List<Emp> result = listEmp.stream() .filter(emp -> emp.getSal() > 1000) //跳过3位 .skip(3) .collect(toList()); System.out.println("result = " + result); 打印输出:result = [Emp(empno=7654, trueOrFalse=false, ename=MARTIN, job=SALESMAN, mgr=7698, hiredate=Mon Sep 28 00:00:00 CST 1981, sal=1250.0, comm=1400, deptno=30), Emp(empno=7698, trueOrFalse=false, ename=BLAKE, job=MANAGER, mgr=7839, hiredate=Fri May 01 00:00:00 CST 1981, sal=2850.0, comm=null, deptno=30), Emp(empno=7782, trueOrFalse=false, ename=CLARK, job=MANAGER, mgr=7839, hiredate=Tue Jun 09 00:00:00 CST 1981, sal=2450.0, comm=null, deptno=10), Emp(empno=7788, trueOrFalse=false, ename=SCOTT, job=ANALYST, mgr=7566, hiredate=Sun Apr 19 00:00:00 CDT 1987, sal=3000.0, comm=null, deptno=20), Emp(empno=7839, trueOrFalse=false, ename=KING, job=PRESIDENT, mgr=null, hiredate=Tue Nov 17 00:00:00 CST 1981, sal=5000.0, comm=null, deptno=10), Emp(empno=7844, trueOrFalse=false, ename=TURNER, job=SALESMAN, mgr=7698, hiredate=Tue Sep 08 00:00:00 CST 1981, sal=1500.0, comm=0, deptno=30), Emp(empno=7876, trueOrFalse=false, ename=ADAMS, job=CLERK, mgr=7788, hiredate=Sat May 23 00:00:00 CDT 1987, sal=1100.0, comm=null, deptno=20), Emp(empno=7902, trueOrFalse=false, ename=FORD, job=ANALYST, mgr=7566, hiredate=Thu Dec 03 00:00:00 CST 1981, sal=3000.0, comm=null, deptno=20), Emp(empno=7934, trueOrFalse=false, ename=MILLER, job=CLERK, mgr=7782, hiredate=Sat Jan 23 00:00:00 CST 1982, sal=1300.0, comm=null, deptno=10)]
.map()方法,获得集合中每一个元素的某个信息时使用,好比咱们只要拿到集合中的全部员工的姓名:
List<Emp> listEmp = empService.listEmp(); List<String> resultList = listEmp.stream() //获得集合中的某一个元素 .map(emp -> emp.getEname()) .collect(toList()); System.out.println("resultList = " + resultList); 打印输出: resultList = [aaa, SMITH, ALLEN, WARD, JONES, MARTIN, BLAKE, CLARK, SCOTT, KING, TURNER, ADAMS, JAMES, FORD, MILLER]
当你不知道流中的方法,.filter()或者是.map()抑或是其余任何流中的方法里面须要传递什么参数返回什么结果的时候,就能够向以前同样,追踪一下源码,咱们以.map()为例:
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
map方法里面须要的是另外一种函数式接口,Function,咱们继续追:
@FunctionalInterface public interface Function<T, R> { /** * Applies this function to the given argument. * * @param t the function argument * @return the function result */ R apply(T t);
能够看到里面的apply方法,是传入任意类型的T,返回的是不一样的任意类型的R,在看咱们的代码,传入Emp,返回String,知足!:
.map(emp -> emp.getEname())
就是.flatMap()方法.这个方法的做用是将一个流中的每一个流都换成另外一个值,而后把全部流链接起来成为一个流.
举个具体的例子.将["Hello","World"] 变为["H","e","l","w","r","d"]
List<String> collect = list.stream() .map(txt -> txt.split("")) .flatMap(txt -> Arrays.stream(txt)) .distinct().collect(toList());
使用.split("")方法返回两个数组{"H","e","l","l","o"}和{"W","o""r","d"}
.map(txt -> txt.split(""))
Arrays.stream()方法将两个数组变成两个流,flatMap将两个流合成一个流.
.flatMap(txt -> Arrays.stream(txt))
Stream API提供allMatch、anyMatch、noneMatch、findFirst和findAny方法.判断集合中是否有要匹配的值.
anyMatch,若是有一个匹配就返回true,看看是否有员工叫SMITH:
List<Emp> list = empService.listEmp(); boolean result = list.stream().anyMatch(emp -> emp.getEname().equals("SMITH")); System.out.println("result = " + result); 输出结果: result = true
allMatch,所有匹配才返回true:
List<Emp> list = empService.listEmp(); boolean result = list.stream().allMatch(emp -> emp.getEname().equals("SMITH")); System.out.println("result = " + result); 输出: result = false
noneMatch,若是没有匹配到返回true:
List<Emp> list = empService.listEmp(); boolean result = list.stream().noneMatch(emp -> emp.getEname().equals("SMITH")); System.out.println("result = " + result); 打印输出: result = false
anyMatch、allMatch和noneMatch这三个操做都用到了咱们所谓的短路,这就是你们熟悉的Java中的&&和||运算符短路在流中的版本.
这里要说明一点,stream分为中间操做和终端操做,像map()、filter()、flatMap()等就是中间操做,他们能够继续调用其它中间操做,想一个流水线同样.而终端操做就是没法再调用其它流的方法的方法,例如 刚刚的三个match方法和collect()方法等.
findAny方法返回当前流中的任意元素,能够将filter和findAny配合使用:
List<Emp> list = empService.listEmp(); Optional<Emp> result = list.stream().filter(emp -> emp.getSal() > 2000).findAny(); System.out.println("result = " + result); System.out.println("result.get() = " + result.get()); 打印输出: result = Optional[Emp(empno=7566, trueOrFalse=false, ename=JONES, job=MANAGER, mgr=7839, hiredate=Thu Apr 02 00:00:00 CST 1981, sal=2975.0, comm=null, deptno=20)] result.get() = Emp(empno=7566, trueOrFalse=false, ename=JONES, job=MANAGER, mgr=7839, hiredate=Thu Apr 02 00:00:00 CST 1981, sal=2975.0, comm=null, deptno=20)
细心的朋友可能发现了,这里返回的结果是一个:
Optional<Emp> result
有了Optional类之后就能够和nullPointerException说88了,这是一个容器类,表明一个值存在或不存在
findFirst方法和findAny差很少,可是若是这是一个并行流,findAny返回的就是最早找到的任意一个,而findFirst返回的是第一个.
reduce()方法,求全部员工的工资之和:
List<Emp> list = empService.listEmp(); Double reduce = list.stream().map(emp -> emp.getSal()) //这里的第一个参数0表明初始值 .reduce((double) 0, (a, b) -> a + b); System.out.println("获得的工资之和是:"+reduce); 打印输出: 获得的工资之和是:29026.0
注意reduce()方法中的第一个参数0也能够不写可是返回的就是一个Optional类型的结果.
List<Emp> list = empService.listEmp(); Optional<Double> reduce = list.stream().map(emp -> emp.getSal()) //这里的第一个参数0表明初始值 .reduce((a, b) -> a + b); System.out.println("获得的工资之和是:"+reduce);
还有一点须要注意的是,若是reduce执行的是乘法,那么初始值就应该是1而不是0.
使用reduce也能够得到最大值和最小值:
List<Emp> list = empService.listEmp(); Optional<Double> reduce = list.stream().map(emp -> emp.getSal()) //得到最大值 .reduce(Double::max); System.out.println("最大工资是::"+reduce.get()); 打印输出: 最大工资是::5000.0
求最小值只需把Double::max换成Double::min.(这是方法引用,看不懂的移步上面的另外一篇博客)
使用Stream.of方法建立一个流.
Stream<String> values = Stream.of("aaa","bbb","ccc"); values.map(String::toUpperCase).forEach(System.out::println); //等价于下面的 values.map(value -> value.toUpperCase()).forEach(value -> System.out.println(value));
两个写法的效果是同样的,一个使用的是lambda表达式一个使用的是方法引用.
int[] values = {1,2,3}; IntStream stream = Arrays.stream(values); //sum方法求和 int sum = stream.sum(); System.out.println("sum = " + sum); 打印输出:sum = 6
Stream.iterate和Stream.generate能够建立无穷无尽的流,可是通常会使用limit()方法来限制建立流的大小,以免打印无数的值,例子:
Stream.iterate(0,n-> n+2) .limit(10) .forEach(System.out::println); 输出: 0 2 4 6 8 10 12 14 16 18
第一次输出的是0,共输出十次.
generate与iterate相似.