Stream串行流程图
2022-12-03 15:11:45 0 举报
List<Author> authorList = getAuthors(); Stream<Author> stream = authorList.stream(); Stream<Author> stream1 = stream.filter(author -> author.getAge() > 10); long count = stream1.count(); System.out.println(count);
作者其他创作
大纲/内容
调用的是AbstractPipeline类的evaluatefinal <R> R font color=\"#1976d2\
Stream<Author> stream = authorList.stream();创建流阶段 stream地址为ReferencePipeline$Head@950
CountingSink 实现AccumulatingSink,AccumulatingSink是ReduceOps的私有接口,而AccumulatingSink又继承TerminalSink,TerminalSink继承sink和Supplier,所以实现了Supplier的public Long get() {return count;}的方法
ReferencePipeline类public final Stream<P_OUT> filter(Predicate<? super P_OUT> predicate) { Objects.requireNonNull(predicate);// this为ReferencePipeline$Head@950// new的地址 ReferencePipeline$2@962 return font color=\"#7b1fa2\
makeSink()
调用的是ReferencePipeline类的count方法public final long count() { return evaluate(ReduceOps.makeRefCounting());}//参数调用ReduceOps类的makeRefCounting()静态方法,new ReduceOp()匿名内部类,实现了TerminalOp接口的四个方法此方法返回TerminalOp接口的子类对象ReduceOps.makeRefCounting()地址是ReduceOps$5@1007
调用AbstractPipeline类的wrapSink方法final <P_IN> Sink<P_IN> wrapSink(Sink<E_OUT> sink) { Objects.requireNonNull(sink); for ( @SuppressWarnings(\"rawtypes\") AbstractPipeline p=AbstractPipeline.this; p.depth > 0; p=p.previousStage) { sink = p.font color=\"#1976d2\
此阶段stream1构建完成,地址为new new StatelessOp()地址ReferencePipeline$2@962
opWrapSink()
List<Author> authorList = getAuthors();Stream<Author> stream = authorList.stream();Stream<Author> stream1 = stream.filter(author -> author.getAge() > 10);long count = stream1.count();System.out.println(count);// 下面用流程图来分析stream创建流程,filter,count过程
List<Author> authorList = getAuthors();authorList地址ArrayList@938
new了一个ReferencePipeline抽象类的静态内部类
调用evaluateParallel
此阶段stream流创建好了
new CountingSink.OfRef<>();实际是new ReduceOps类中抽象静态内部类CountingSink继承box实现AccumulatingSink在此内部中有静态终态类new的地址ReduceOps$CountingSink$OfRef@999static final class OfRef<T> extends CountingSink<T> { @Override public void accept(T t) { count++;// 实际调用是此方法 } }
Stream<Author> stream1 = stream.filter(author -> author.getAge() > 10);调用new出来Head父类的ReferencePipeline的filter方法同时传入new Predicate()匿名内部类地址类名$Lambda@961
long count = stream1.count();ReferencePipeline$2@962调用继承父类的count方法
ArrayList$ArrayListSpliterator@949的font color=\"#ff9800\
new 对象font color=\"#f44336\
makeSink()调用的是ReduceOps$5@1007的makeSink() new CountingSink.OfRef<>();
sink ReduceOps$CountingSink$OfRef@999new Sink.ChainedReference(sink)new出来的地址ReferencePipeline$2$1@998// Sink接口的抽象静态内部类ChainedReference
该接口需要调用ArrayList的splicerator()地址为font color=\"#f44336\
![Stream中findAny](https://www.processon.com/chart_image/template/thumb/638db1b876213156374c3306.png?tid=638af9caf346fb139e010703)
收藏
![Stream串行流程图](https://www.processon.com/chart_image/template/thumb/638daa4976213156374c2a62.png?tid=638ab2511e08536bffa2dc07)
收藏
0 条评论
回复 删除
下一页