Stream中findAny
2022-12-05 10:46:36 1 举报
Stream<Author> stream = getAuthors().stream(); Stream<Author> stream1 = stream.filter(author -> author.getAge() > 40); Stream<String> stream2 = stream1.map(new Function<Author, String>() { @Override public String apply(Author author) { return author.getName(); } }); Optional<String> stream2Any = stream2.findAny(); String name = stream2Any.orElseGet(() -> "佚名"); System.out.println(name);
作者其他创作
大纲/内容
ReferencePipeline$Head@967的ReferencePipeline的forEachWithCancel方法传入ReferencePipeline$2$1@999 filter的spliterator font color=\"#f44336\
ReferencePipeline$3@978的AbstractPipeline的 wrapSinksink FindOps$FindSink$OfRef@995final <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) {// p指向上一阶段 sink = p.font color=\"#2196f3\
注意:因为一级一级向前调用操作符,所以当第一个操作符即filter为true的时候就会返回,之后的数据都不会匹配,也就是说findAny方法总是获取到的是filter第一个匹配的值,因此可能不会遍历完整个数组,短路操作
AbstractPipeline 方法 evaluateterminalOp地址 font color=\"#f44336\
调ReferencePipeline$3@978的AbstractPipeline 的copyInto方法传入ReferencePipeline$2$1@999 filter的spliterator font color=\"#f44336\
调ReferencePipeline$3@978的AbstractPipeline 的copyIntoWithCancel方法传入ReferencePipeline$2$1@999 filter的spliterator font color=\"#f44336\
Stream<String> stream2 = stream1.map(author -> author.getName());// stream2 ReferencePipeline$3@978// 类$lambda@977Optional<String> stream2Any = stream2.findAny();// Optional@1037
调用AbstractPipeline抽象类的evaluate方法,和filter调用的方法一样
调findAny()
font color=\"#f44336\
List<Author> authorList = getAuthors();Stream<Author> stream = authorList.stream();Stream<Author> stream1 = stream.filter(author -> author.getAge() > 10);long count = stream1.count();Stream<String> stream2 = stream1.map(author -> author.getName());Optional<String> stream2Any = stream2.findAny();String name = stream2Any.orElseGet(() -> \"佚名\");System.out.println(count);System.out.println(name);// 错误用法
第一次map的opWrapSink
分别调用map ReferencePipeline$3$1@998的cancellationRequestedpublic boolean cancellationRequested() { return downstream.cancellationRequested(); }downstream findAny FindOps$FindSink$OfRef@995
List<Author> authorList = getAuthors();//ArrayList@725,分割迭代器对象ArrayList$ArrayListSpliterator@960Stream<Author> stream = authorList.stream();// ReferencePipeline$Head@967//类名$lambda@972Stream<Author> stream1 = stream.filter(author -> author.getAge() > 10);// stream1 ReferencePipeline$2@973
分别调用filterReferencePipeline$2$1@999的cancellationRequestedpublic boolean cancellationRequested() { return downstream.cancellationRequested(); }downstream map ReferencePipeline$3$1@998
new FindOp
helper调ReferencePipeline$3@978的AbstractPipeline 的wrapAndCopyInto方法sink FindOps$FindSink$OfRef@995spliterator font color=\"#f44336\
调ReferencePipeline抽象类的map方法new StatelessOp() stream2 ReferencePipeline$3@978
匿名对象 类$lambda@1159String name = stream2Any.orElseGet(() -> \"佚名\");System.out.println(name);
ArrayList终态内部类ArrayListSpliteratorthis ArrayList$ArrayListSpliterator@960传入ReferencePipeline$2$1@999public boolean font color=\"#0097a7\
看同个流中间操作
public ChainedReference(Sink<? super E_OUT> downstream) { this.downstream = Objects.requireNonNull(downstream); }downstream地址ReferencePipeline$3$1@998
调FindOps类中FindSink内部静态抽象类OfRef中的静态属性font color=\"#388e3c\
List<Author> authorList = getAuthors();Stream<Author> stream = authorList.stream();Stream<Author> stream1 = stream.filter(author -> author.getAge() > 10);Stream<String> stream2 = stream1.map(author -> author.getName());Optional<String> stream2Any = stream2.findAny();String name = stream2Any.orElseGet(() -> \"佚名\");System.out.println(name);
第二次filter的opWrapSink
Optional类的orElseGetpublic T orElseGet(Supplier<? extends T> supplier) { return value != null ? value : supplier.get(); }
public ChainedReference(Sink<? super E_OUT> downstream) { this.downstream = Objects.requireNonNull(downstream); }downstream地址FindOps$FindSink$OfRef@995
new FindOps中私有静态终态内部类FindOp实现font color=\"#388e3c\
Optional::isPresentnew Predicate匿名内部类实现test方法,返回判断接口的isPresent()new Predicate<Optional<Object>>() {@Overridepublic boolean test(Optional<Object> o) {return o.isPresent();}}FindOps$FindSink$OfRef$lambda@981
do { } while (!(cancelled = sink.cancellationRequested()) && spliterator.font color=\"#0097a7\
downstream.accept(mapper.apply(u));mapper地址map(lambda表达式地址返回name)类$lambda@977在调用downstream FindOps$FindSink$OfRef@995的accept即FindOps类的私有抽象静态类FindSink的public void accept(T value) { if (!hasValue) { hasValue = true;// 对hasValue设置为true,避免forEachWithCancelfont color=\"#212121\
List<Author> authorList = getAuthors();//font color=\"#f44336\
FindOps类的私有抽象静态类的FindSinkpublic boolean cancellationRequested() { return hasValue;// 构建findAny的时候初始默认值 false }
调ReferencePipeline抽象类的findAny()public final Optional<P_OUT> findAny() { return evaluate(FindOps.makeRef(false)); }FindOps.makeRef(false)地址FindOps$FindOp@979
mustFindFirst=false
ReferencePipeline$3$1@998指向的是ReferencePipeline的map的opWrapSink()的new Sink.ChainedReference匿名内部类的public void accept(P_OUT u) {downstream.accept(mapper.apply(u));}downstream FindOps$FindSink$OfRef@995
由于和filter一样这里就不赘述了,写生成的类记录有区别的地方
Stream<String> stream2 = stream1.map(author -> author.getName());Optional<String> stream2Any = stream2.findAny();String name = stream2Any.orElseGet(() -> \"佚名\");
Stream<String> stream2 = stream1.map(author -> author.getName());
调FindOps类的静态方法makeRefmustFindFirst=falsefont color=\"#f44336\
ReferencePipeline$2$1@999指向的是ReferencePipeline的filter的opWrapSink()的new Sink.ChainedReference匿名内部类的public void accept(P_OUT u) {if (predicate.test(u))downstream.accept(u);}downstream ReferencePipeline$3$1@998
收藏
收藏
0 条评论
下一页
为你推荐
查看更多