Stream流 (全方位 分析源码)




并行流采用链式编程 lambda表达式 Stream流式计算 函数式接口

1.一个数组怎么转化成了流?

在JDK1.8以后,加入流stream

//Collection接口中也加入转换方法
List<String> list = new ArrayList<String>();
Stream<String> parallelStream = list.stream().........
Stream<String> parallelStream = list.parallelStream()......

这是一个list集合 通过stream()方法变成串行流,
			通过parallelStream()方法变成并行流。

1.1走一遍源码

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.BaseStream

先看一下继承BaseStream的接口
在这里插入图片描述

2.1认识一下BaseStream

public interface BaseStream<T, S extends BaseStream<T, S>>
        extends AutoCloseable {
    
    
    /**
     * Returns an iterator for the elements of this stream.
     * 返回此流元素的迭代器
     */
    Iterator<T> iterator();

    /**
     * Returns a spliterator for the elements of this stream.
     * 返回此流元素的拆分器。
     */
    Spliterator<T> spliterator();

    /**
     * 此流是否将并行执行?
     */
    boolean isParallel();

    /**
     * 返回一个等价的串行流,可能是它自己,因为它已经是串行流,基础流状态被修改为连续的。
     */
    S sequential();

    /**
     * 返回一个等价的并行流。可能会返回本身,或者因为流已经是平行的,或者因为底层流状态被修改为并行。
     */
    S parallel();

    /**
     * 返回一个无序流
     */
    S unordered();

    /**
     * 关闭运行 时 处理程序的流
     */
    S onClose(Runnable closeHandler);

    /**
     * 关闭此流
     */
    @Override
    void close();
}

3.Stream流

Stream接口是继承于BaseStream
public interface Stream<T> extends BaseStream<T, Stream<T>>	

3.1Stream可以直接转化成其他三种流IntStream LongStream DoubleStream

在这里插入图片描述
通过这三个方法,直接转换

    IntStream mapToInt(ToIntFunction<? super T> mapper);

    LongStream mapToLong(ToLongFunction<? super T> mapper);

    DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper);

点入LongStream接口
在这里插入图片描述

3.2 使用四种函数式接口的方法

//过滤 断定型接口
Stream<T> filter(Predicate<? super T> predicate);

//函数型 
<R> Stream<R> map(Function<? super T, ? extends R> mapper);

//将多个Stream连接成一个Stream,这时候不是用新值取代Stream的值,与map有所区别,这是重新生成一个Stream对象取而代之。
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper);
LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper);
DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper);

//排序 (和断定型是否排序)
Stream<T> sorted();
Stream<T> sorted(Comparator<? super T> comparator);

//stream.peek的操作是返回一个新的stream的,且设计的初衷是用来debug调试的,因此使用steam.peek()必须对流进行一次处理再产生一个新的stream。
Stream<T> peek(Consumer<? super T> action);

//传入消费 不需要进行别的操作 无返回
void forEach(Consumer<? super T> action);

// 1.恒等式累积函数的恒等式值
// 2.函数式接口 传入2个参数,返回一个参数
T reduce(T identity, BinaryOperator<T> accumulator);
等等方法

XXXStream接口(LongStream)

//需要传入开始节点和结束节点两个参数,返回的是一个有序的LongStream。
//包含开始节点和结束节点两个参数之间所有的参数,间隔为1.
range(long startInclusive, final long endExclusive)

//和上面方法类似,只是包含了endExclusive边界值
rangeClosed(long startInclusive, final long endExclusive)

//转换成并行流
parallel()

等等

了解下ForkJoin框架

在ForkJoin框架下,进行使用
ForkJoin框架

大量数据中:效率Stream并行流>ForkJoin>for

少了数据中:for>效率Stream并行流>ForkJoin

查看注释基本就可以了解大部分。

猜你喜欢

转载自blog.csdn.net/jj89929665/article/details/112995227