Gitchat连接
https://gitbook.cn/gitchat/activity/5f85696aad812d16b498848c
简介
从底层,从原理,我们来重学一次 Java。Stream 是JDK8中新引入的,方便了数据列表的过滤、投影、遍历等各种处理,他的源码及实现是怎样的呢?
本系列秉承所有结论尽量从源码中来,没有源码的尽量标明出处。相关源码会附着在文章中,读本文即可,不用再自行查找源码及资料学习,方便大家充分利用路上的碎片时间。
本篇 Chat 从Stream示例开始,讲解构建Stream源码,分析了非并行forEach各种情况的源码逻辑,讲解了Stream的各个接口及应用示例,帮助大家深入理解和学习 JDK 源码。
本文包含以下内容:
- 列表获取流
- Collection.stream()
- 数组获取流
- Arrays.stream(T[] array)
- Arrays.stream(T[] array, int startInclusive, int endExclusive)
- 直接构建流
- Stream.of
- StreamSupport
- 构造函数
- stream(Spliterator spliterator, boolean parallel)
- Collection.spliterator()
- fail-fast快速失败机制
- 非并行forEach
- stream直接进行forEach
- Arrays.stream的forEach
- Arrays.spliterator(T[] array, int startInclusive, int endExclusive)
- Spliterators.spliterator(Object[] array, int fromIndex, int toIndex, int additionalCharacteristics)
- ArraySpliterator.forEachRemaining(Consumer<? super T> action)
- 总结
- ArrayList stream的forEach
- Collection.stream()
- ArrayList.spliterator()
- ArrayList.ArrayListSpliterator
- ArrayList.ArrayListSpliterator.forEachRemaining
- 总结
- Arrays.stream的forEach
- stream进行一次中间操作再forEach
- ReferencePipeline.filter
- ReferencePipeline.StatelessOp
- ReferencePipeline.forEach
- ForEachOps
- ForEachOps.makeRef(Consumer<? super T> action, boolean ordered)
- ForEachOp
- ForEachOp.OfRef
- AbstractPipeline.evaluate
- ForEachOps.ForEachOp.evaluateSequential
- AbstractPipeline.wrapAndCopyInto
- AbstractPipeline.wrapSink
- AbstractPipeline.copyInto
- Sink.ChainedReference
- 总结
- stream直接进行forEach
- Stream
- 接口定义
- 接口继承结构
- filter(Predicate<? super T> predicate)
- map(Function<? super T, ? extends R> mapper)
- mapToInt(ToIntFunction<? super T> mapper)
- mapToLong(ToLongFunction<? super T> mapper)
- mapToDouble(ToDoubleFunction<? super T> mapper)
- flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)
- flatMapToInt(Function<? super T, ? extends IntStream> mapper)
- flatMapToLong(Function<? super T, ? extends LongStream> mapper)
- flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper)
- distinct()
- sorted()
- sorted(Comparator<? super T> comparator)
- peek(Consumer<? super T> action)
- limit(long maxSize)
- skip(long n)
- forEach(Consumer<? super T> action)
- forEachOrdered(Consumer<? super T> action)
- toArray(IntFunction<A[]> generator)
- reduce(T identity, BinaryOperator accumulator)
- reduce(BinaryOperator accumulator)
- reduce(U identity,BiFunction<U, ? super T, U> accumulator,BinaryOperator<U> combiner)
- 调用示例1
- 调用示例2
- 调用示例3
- collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner)
- 调用示例1
- 调用示例2
- 调用示例3
- collect(Collector<? super T, A, R> collector)
- 流转List示例
- GroupBy分组示例
- min(Comparator<? super T> comparator)
- max(Comparator<? super T> comparator)
- count()
- anyMatch(Predicate<? super T> predicate)
- 调用示例1
- 调用示例2
- allMatch(Predicate<? super T> predicate)
- 调用示例1
- 调用示例2
- noneMatch(Predicate<? super T> predicate)
- 调用示例1
- 调用示例2
- findFirst()
- findAny()
- builder()
- empty()
- of(T t)
- Stream<T> of(T… values)
- iterate(final T seed, final UnaryOperator<T> f)
- Streams.NONE
- Spliterators.spliteratorUnknownSize(Iterator<? extends T> iterator, int characteristics)
- 调用示例
- generate(Supplier s)
- StreamSpliterators
- StreamSpliterators.InfiniteSupplyingSpliterator
- StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<T>
- 调用示例
- concat(Stream<? extends T> a, Stream<? extends T> b)
- Streams.composedClose(BaseStream<?, ?> a, BaseStream<?, ?> b)
- Throwable.addSuppressed(Throwable exception)
- Throwable.SUPPRESSED_SENTINEL
- Throwable.suppressedExceptions
- Stream.Builder
- accept(T t)
- add(T t)
- build()
- BaseStream
- 接口定义
- 接口继承结构
- AutoCloseable
- 接口定义
- 接口继承结构
- close()
- try-with-resources