JAVA新特性(7)Stream接口源码分析

本文深入分析JAVA Stream接口,讲解其作为AutoCloseable接口子类的特性。通过Spliterator接口的介绍,阐述IntConsumer的概念,并提供示例。文章还探讨了流的计算本质,不关注数据本身,而关注计算过程。同时,详细剖析了ReferencePipeline作为流源阶段和中间阶段的实现,以及Lambda表达式与匿名内部类的区别。在源码层面,展示了map和forEach等操作的实现细节。
摘要由CSDN通过智能技术生成
  1. Stream接口继承自BaseStream继承了AutoCloseable接口
    【补充】AutoCloseable接口说明:
    * <p>Streams have a {@link #close()} method and implement {@link AutoCloseable},
    * but nearly all stream instances do not actually need to be closed after use.
    * Generally, only streams whose source is an IO channel (such as those returned
    * by {@link Files#lines(Path, Charset)}) will require closing.  Most streams
    * are backed by collections, arrays, or generating functions, which require no
    * special resource management.  (If a stream does require closing, it can be
    * declared as a resource in a {@code try}-with-resources statement.)
    public class AutoCloseableTest implements AutoCloseable{
    //    测试AutoCloseable接口的用法:使用try with resources声明时 接口中的方法会被自动的调用
    //    比如操作文件,IO流时都创建新的文件,IO流,使用try中创建新的对象(即 with resources),
    //    然后对文件内容或IO内容进行操作时就会自动调用close方法,以关闭资源
        public void doSomething(){
            System.out.println("do some things");
        }
        public void close() throws Exception{
            System.out.println("close invoked");
        }
    
        public static void main(String[] args) throws Exception {
            try (AutoCloseableTest autoCloseable = new AutoCloseableTest()){
                autoCloseable.doSomething();//输出:do some things  close invoked
            }
        }
        @Test
        public void Test1(){
    //        读BaseStream接口中onClose方法上面的说明文档。测试它的功能或作用:
            List<String> list = Arrays.asList("Hello","World","Hello world");
            try(Stream<String> stream = list.stream() ){
                stream.onClose(()-> {
                    System.out.println("close");
                    throw new NullPointerException("first exception");
    //                throw  NullPointerException;
                }).onClose(()-> {
                    System.out.println("close1");
                    throw new NullPointerException("second exception");
    //                throw  NullPointerException;
                }).forEach(System.out::println);
            }
        }
    }

     

  2. JAVA DOC需要认真看。
  3. Spliterator接口介绍与源码分析
     * @apiNote
     * <p>Spliterators, like {@code Iterator}s, are for traversing the elements of
     * a source.  The {@code Spliterator} API was designed to support efficient
     * parallel traversal in addition to sequential traversal, by supporting
     * decomposition as well as single-element iteration.  In addition, the
     * protocol for accessing elements via a Spliterator is designed to impose
     * smaller per-element overhead than {@code Iterator}, and to avoid the inherent
     * race involved in having separate methods for {@code hasNext()} and
     * {@code next()}.
    //最后一句:Spliterator避免了在使用hasNext和next[他们总是连续使用,先用hasNext判断是否存在然后再next] 
    //时当多个线程来操作时就可能会的出现问题;
     * <p>Primitive subtype specializations of {@code Spliterator} are provided for
     * {@link OfInt int}, {@link OfLong long}, and {@link OfDouble double} values.
     * The subtype default implementations of
     * The subtype default implementations of
     * {@link Spliterator#tryAdvance(java.util.function.Consumer)}
     * and {@link Spliterator#forEachRemaining(java.util.function.Consumer)} box
     * primitive values to instances of their corresponding wrapper class.  Such
     * boxing may undermine any performance advantages gained by using the primitive
     * specializations.  To avoid boxing, the corresponding primitive-based methods
     * should be used.  For example,
     * {@link Spliterator.OfInt#tryAdvance(java.util.function.IntConsumer)}
     * and {@link Spliterator.OfInt#forEachRemaining(java.util.function.IntConsumer)}
    //提供了原始接口(primitive)的子类型(int lo0ng double)的特化接口:
       避免了装箱与拆箱带来的性能损耗
    
     * <p><a name="binding">A Spliterator that does not report {@code IMMUTABLE} or
     * {@code CONCURRENT} is expected to have a documented policy concerning:
     * when the spliterator <em>binds</em> to the element source; and detection of
     * structural interference of the element source detected after binding.</a>  A
     * <em>late-binding</em> Spliterator binds to the source of elements at the
     * point of first traversal, first split, or first query for estimated size,
     * rather than at the time the Spliterator is created.  A 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值