java8:IntStream,LongStream,DoubleStream等操作

summaryStatistics

主要用于统计整形数组中元素的最大值,最小值,平均值,个数,元素总和等等。下面是一个简单的例子:

 int[] intArray = {12,3,34,67,100,99};
        /** 第一种构造intStream **/
        IntStream intStream = IntStream.of(intArray);
        /** 第二种构造intStream **/
        //IntStream intStream2 = IntStream.of(12,3,34,67,100,99);
        /** 这个是重点,获得当前int数组的统计信息,包括 **/
        IntSummaryStatistics statistics = intStream.summaryStatistics();
        /** 计算出最大,最小,平均等等,是不是很好用,赶紧get起来 **/
        System.out.println("the max:" + statistics.getMax());
        System.out.println("the min:" + statistics.getMin());
        System.out.println("the average:" + statistics.getAverage());
        System.out.println("the sum:" + statistics.getSum());
        System.out.println("the count:" + statistics.getCount());

将数值流转回对象流

@Test
public void testToInt() {
    final ArrayList<Dish> dishes = Lists.newArrayList(
            new Dish("pork", false, 800, Type.MEAT),
            new Dish("beef", false, 700, Type.MEAT),
            new Dish("chicken", false, 400, Type.MEAT),
            new Dish("french fries", true, 530, Type.OTHER),
            new Dish("rice", true, 350, Type.OTHER),
            new Dish("season fruit", true, 120, Type.OTHER),
            new Dish("pizza", true, 550, Type.OTHER),
            new Dish("prawns", false, 300, Type.FISH),
            new Dish("salmon", false, 450, Type.FISH)
    );

    IntStream intStream = dishes.stream()
            .mapToInt(Dish::getCalories);
}

我们虽然会使用数值流进行计算,但经常需要回归到对象,那么就需要将int stream装箱为Integer stream. 可以使用boxed()方法。

Stream<Integer> boxed = intStream.boxed();

生成一个数值范围流rangeClosed,range

有时候需要生成一个数值范围,比如1到30. 可以使用for循环,也可以直接使用数值流。

创建一个包含两端的数值流,比如1到10,包含10:

IntStream intStream = IntStream.rangeClosed(1, 10);

创建一个不包含结尾的数值流,比如1到10: 不包含10

IntStream range = IntStream.range(1, 10);

sequential / parallel

sequential 返回的流是串行处理,parallel返回的流是并行处理,参考如下测试用例:

@Test
    public void test12() throws Exception {
        IntStream intStream = IntStream.of(6, 1, 1, 2, 5, 2, 3, 4);
        long start=System.currentTimeMillis();
        //并行处理
        intStream.parallel().forEach(x->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+":"+x);
        });
        System.out.println("parallel time->"+(System.currentTimeMillis()-start));
 
        intStream = IntStream.of(6, 1, 1, 2, 5, 2, 3, 4);
        start=System.currentTimeMillis();
        //默认都是串行处理
        intStream.sequential().forEach(x->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+":"+x);
        });
        System.out.println("sequential time->"+(System.currentTimeMillis()-start));
    }

concat 合并流

/*
* Stream流中的常用方法concat:用于合并两个流
  *  */
public class demolimit {
    public static void main(String[] args) {
        String[] strings = {"meiyangyang","xiyangang","lanyangyang"};
        Integer[] integers = {1,2,3};
        Stream stream = Stream.of(strings);
        Stream stream1 = Stream.of(integers);
        Stream stream2 = Stream.concat(stream,stream1);
        stream2.forEach(s->System.out.println(s));
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值