Java Stream

入门

Java Stream,一个号称能够极大提高程序员开发效率的神器, 一个学会了都不想写for循环的好东西。程序员

本质上是一种函数式编程思惟,是将要处理的元素集合看做一种流, 流在管道中传输,进行各类操做。编程

这是官网给出的Stream的特性:bash

Sequence of elements − A stream provides a set of elements of specific type in a sequential manner. A stream gets/computes elements on demand. It never stores the elements.

Source − Stream takes Collections, Arrays, or I/O resources as input source.

Aggregate operations − Stream supports aggregate operations like filter, map, limit, reduce, find, match, and so on.

Pipelining − Most of the stream operations return stream itself so that their result can be pipelined. These operations are called intermediate operations and their function is to take input, process them, and return output to the target. collect() method is a terminal operation which is normally present at the end of the pipelining operation to mark the end of the stream.

Automatic iterations − Stream operations do the iterations internally over the source elements provided, in contrast to Collections where explicit iteration is required.
复制代码

概念

中间操做

(intermediate operation)ide

调用中间操做只会生成一个标记了该操做的新stream,能够继续执行函数式编程

中间操做主要包括有:函数

  • concat()
  • distinct()
  • filter()
  • flatMap()
  • limit()
  • map()
  • peek()
  • skip()
  • sorted()
  • parallel()
  • sequential()
  • unordered()

最终操做

(terminal operation)ui

元素流在管道中通过中间操做的处理,最后由最终操做获得前面处理的结果。spa

最终操做主要有:code

  • allMatch()
  • anyMatch()
  • collect()
  • count()
  • findAny()
  • findFirst()
  • forEach()
  • forEachOrdered()
  • max()
  • min()
  • noneMatch()
  • reduce()
  • toArray()

使用举例

查找最大最小值

List<T> list = new LinkedList<>();
     ...
     //最小值
    Optional<T> min = 
        list.stream()
            .min(Comparator.comparing(T::getScore));
    if(min.isPresent()){
        T tMin = min.get();
    }
        
    //最大值    
    Optional<T> max =      
        list.stream()
            .max(Comparator.comparing(T::getScore));
    if(max.isPresent()){
        T tMax = t.get();
    }
复制代码

遍历打印

List<T> list = new LinkedList<>();
     ...
     list.stream()
         .forEach(
                i ->System.out.println(i));
复制代码

分页

List<T> list = new LinkedList<>();
     ...
    List<T> pagedList = list.stream()
                            .skip(offset)
                            .limit(limit)
                            .collect(Collectors.toList());
复制代码

List 转 Map

下标index为keyorm

List<T> list = new LinkedList<>();
Map<Long, Integer> map = IntStream.range(0, list.size())
                                  .boxed()
                                  .collect(Collectors.toMap(i -> i,i -> list.get(i), (p1,p2)->p1));

复制代码

提取List中某字段

List<T> list = new LinkedList<>();
    ...
    List<Integer> newList = list.stream()
                                .map(T::getField)
                                .collect(Collectors.toList());
复制代码

List<List> to List

List<List<T>> list = new LinkedList<>();
    ...
    List<Long> collectIds = list.stream()
                                .flatMap(List::stream)
                                .collect(Collectors.toList());
复制代码
相关文章
相关标签/搜索