java.util.Arrays相关源码阅读-----stream方法

1. Stream<T> stream(T[] array) 

        该方法为一个静态泛型方法,根据具体入参来决定返回哪种类型的流。

public static <T> Stream<T> stream(T[] array) {
        return stream(array, 0, array.length);
    }

2.Stream<T> stream(T[] array, int startInclusive, int endExclusive) 

        该方法同上一个方法多了两个形参:

        startinclusive:数组array想从哪个位置作为转换流的开始。

        endExclusive:数组array想从哪个位置作为转换流的结束。

        首先,方法先调用spliterator方法,这个方法介绍之前,首先是Spliterator是Java8的一个新街口,作用就是将顺序执行的任务变成多个子任务从而便于并行流,交给多线程进行完成。

 public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive) {
        return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false);
    }

        spliterator方法首先检查原来数组array是否为空,并且检查开始位置和结束位置是否有越界,之后返回一个ArraySpliterator对象,Spliterator接口已经为各个集合框架提供了对应的可拆分迭代器Spliterator。

public static <T> Spliterator<T> spliterator(Object[] array, int fromIndex, int toIndex,
int additionalCharacteristics) {
checkFromToBounds(Objects.requireNonNull(array).length, fromIndex, toIndex);
return new ArraySpliterator<>(array, fromIndex, toIndex, additionalCharacteristics);
}

        接着回到stream方法,之后调用StreamSupport的静态方法stream,该方法的第一个入参为刚才方法执行生成的数组集合的Spliterator对象,而第二个入参则是代表转换出来的流是顺序流还是并行流,这里Arrays类调用的默认为顺序流。(并行流并不是永远比顺序流要快,如果本身任务数量就很少,那么选取并行流反而增加了机器负担)。最后,通过管道流来将对应的spliterator对象转换输出成对应格式的流对象。

public static <T> Stream<T> stream(Spliterator<T> spliterator, boolean parallel) {
        Objects.requireNonNull(spliterator);
        return new ReferencePipeline.Head<>(spliterator,
                                            StreamOpFlag.fromCharacteristics(spliterator),
                                            parallel);
    }

3.IntStream stream(int[] array) 

        由于面向对象编程的多态性,若入参为int数组类型,则调用上述方法生成一个IntStream流对象。

public static IntStream stream(int[] array) {
        return stream(array, 0, array.length);
    }

4.IntStream stream(int[] array, int startInclusive, int endExclusive) 

        该方法如上述一致,并且输出的为顺序流而非并行流。

public static IntStream stream(int[] array, int startInclusive, int endExclusive) {
        return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false);
    }

5.LongStream stream(long[] array) 

public static LongStream stream(long[] array) {
        return stream(array, 0, array.length);
    }

6.LongStream stream(long[] array, int startInclusive, int endExclusive) 

public static LongStream stream(long[] array, int startInclusive, int endExclusive) {
return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false);
}

7.DoubleStream stream(double[] array)

public static DoubleStream stream(double[] array) {
return stream(array, 0, array.length);
}

8.DoubleStream stream(double[] array, int startInclusive, int endExclusive) 

public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) {
return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false);
}

上述3-8为Arrays为编程人员封装好的具体类型的流对象方法,剩下的可以通过1和2方法进行灵活运用。值得注意的是,这里的DoubleStream,LongStream,IntStream流与Stream<Integer>等是不一样的,前者是int,long,double基础类型的流对象,而后者则是Integer,Long,Double的对象流,前者不能直接转换成后者,而是通过boxed()方法转换成后者。而后者转换呈前者要通过stream.mapToInt(Integer::intValue)来进行相关转换。

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值