zoukankan      html  css  js  c++  java
  • J2SE 8的流库 --- 基本类型流的使用

    展现流的方法


    public static <T> void show(String title, Stream<T> stream){
    	System.out.println("title:"+title);
    	
    	List<T> collect = stream.limit(10).collect(Collectors.toList());
    	collect.forEach(n->System.out.println(n));
    	System.out.println();
    }

    创建公共的list

    ArrayList<String> arrayList = new ArrayList<>();
    arrayList.add("aa");
    arrayList.add("bb");
    arrayList.add("cc");
    
    Random random = new Random();
    1. IntStream
    //(1)创建IntStream
    IntStream intStream = IntStream.of(1,1,2,3,4);
    show("intStream", intStream.boxed());		//boxed()	基本类型流->对象流
    
    intStream = IntStream.range(0, 5);		//[int startInclusive, int endExclusive),不包括5
    show("intStream", intStream.boxed());
    
    intStream = IntStream.rangeClosed(0, 5);	//[int startInclusive, int endExclusive],包括5
    show("intStream", intStream.boxed());
    
    intStream = IntStream.generate(()->new Random().nextInt());
    show("intStream", intStream.boxed());
    
    intStream = IntStream.iterate(0, n->n+8);
    show("intStream", intStream.boxed());
    
    IntStream mapToIntStream = arrayList.stream().mapToInt(String::length);		//mapToInt	将对象流->基本类型流
    show("mapToIntStream", mapToIntStream.boxed());
    
    //codePoints->IntStream
    IntStream codePointsIntStream = "世界上很好听的纯音乐(经典不朽)".codePoints();
    show("codePointsIntStream", codePointsIntStream.boxed());	
    
    //charsIntStream->IntStream
    IntStream charsIntStream = "世界上很好听的纯音乐(经典不朽)".chars();
    show("charsIntStream", charsIntStream.boxed());
    
    //产生随机数流
    IntStream randomIntStream = random.ints();
    show("randomIntStream", randomIntStream.boxed());
    
    randomIntStream = random.ints(3);			//指定stream size 3
    show("randomIntStream", randomIntStream.boxed());
    
    randomIntStream = random.ints(10, 15);		//指定起点10, 边界15
    show("randomIntStream", randomIntStream.boxed());
    
    randomIntStream = random.ints(8, 10, 15);	//指定stream size 8, 指定起点10, 边界15
    show("randomIntStream", randomIntStream.boxed());
    
    
    
    
    //(2)得到平均值, 和, 最大值, 最小值 
    //使用Supplier<T> 包装流,防止得到流已使用/关闭错误
    Supplier<IntStream> intStreamSupplier = () -> IntStream.of(1,1,2,3,4);
    System.out.println("intStream.average()		"+intStreamSupplier.get().average().getAsDouble());
    System.out.println("intStream.sum()			"+intStreamSupplier.get().sum());
    System.out.println("intStream.max()			"+intStreamSupplier.get().max().getAsInt());
    System.out.println("intStream.min()			"+intStreamSupplier.get().min().getAsInt());
    
    IntSummaryStatistics intSummaryStatistics = intStreamSupplier.get().summaryStatistics();
    System.out.println("intStream.average()		"+intSummaryStatistics.getAverage());
    System.out.println("intStream.sum()			"+intSummaryStatistics.getSum());
    System.out.println("intStream.max()			"+intSummaryStatistics.getMax());
    System.out.println("intStream.min()			"+intSummaryStatistics.getMin());
    System.out.println();
    
    
    //(3) IntStream -> 数组
    int[] arrayIntStream = intStreamSupplier.get().toArray();
    System.out.println("arrayIntStream	"+Arrays.toString(arrayIntStream));
    System.out.println();
    
    
    //(4) mapToObjStream
    Stream<String[]> mapToObjStream = IntStream.range(0, 15).mapToObj(x->new String[]{"asdas","vfvfvc","43fg","fgfg"});
    mapToObjStream.forEach((x)->System.out.println(Arrays.toString(x)));
    System.out.println();
    2. LongStream
    //(1)创建LongStream
    LongStream longStream = LongStream.of(1,1,2,3,4);
    show("longStream", longStream.boxed());		//boxed()	基本类型流->对象流
    
    longStream = LongStream.range(0, 5);		//[int startInclusive, int endExclusive),不包括5
    show("longStream", longStream.boxed());
    
    longStream = LongStream.rangeClosed(0, 5);	//[int startInclusive, int endExclusive],包括5
    show("longStream", longStream.boxed());
    
    longStream = LongStream.generate(()->new Random().nextLong());
    show("longStream", longStream.boxed());
    
    longStream = LongStream.iterate(0, n->n+8);
    show("longStream", longStream.boxed());
    
    LongStream mapToLongStream = arrayList.stream().mapToLong(x->(long)x.length());		//mapToLong	将对象流->基本类型流
    show("mapToLongStream", mapToLongStream.boxed());	
    
    //产生随机数流
    LongStream randomLongStream = random.longs();
    show("randomLongStream", randomLongStream.boxed());
    
    randomLongStream = random.longs(3);				//指定stream size 3
    show("randomLongStream", randomLongStream.boxed());
    
    randomLongStream = random.longs(10, 15);		//指定起点10, 边界15
    show("randomLongStream", randomLongStream.boxed());
    
    randomLongStream = random.longs(8, 10, 15);		//指定stream size 8, 指定起点10, 边界15
    show("randomLongStream", randomLongStream.boxed());
    
    
    
    //(2)得到平均值, 和, 最大值, 最小值 
    //使用Supplier<T> 包装流
    Supplier<LongStream> longStreamSupplier = () -> LongStream.of(1,1,2,3,4);
    System.out.println("longStream.average()		"+longStreamSupplier.get().average().getAsDouble());
    System.out.println("longStream.sum()			"+longStreamSupplier.get().sum());
    System.out.println("longStream.max()			"+longStreamSupplier.get().max().getAsLong());
    System.out.println("longStream.min()			"+longStreamSupplier.get().min().getAsLong());
    
    LongSummaryStatistics longSummaryStatistics = longStreamSupplier.get().summaryStatistics();
    System.out.println("longStream.average()		"+longSummaryStatistics.getAverage());
    System.out.println("longStream.sum()			"+longSummaryStatistics.getSum());
    System.out.println("longStream.max()			"+longSummaryStatistics.getMax());
    System.out.println("longStream.min()			"+longSummaryStatistics.getMin());
    System.out.println();
    
    
    //(3) LongStream -> 数组
    long[] arrayLongStream = longStreamSupplier.get().toArray();
    System.out.println("arrayLongStream	"+Arrays.toString(arrayLongStream));
    System.out.println();
    3. DoubleStream
    //(1)创建DoubleStream
    DoubleStream doubleStream = DoubleStream.of(1.1,1.2,1.3,2.1,2.2);
    show("doubleStream", doubleStream.boxed());
    
    doubleStream = DoubleStream.generate(Math::random);
    show("doubleStream", doubleStream.boxed());
    
    doubleStream = DoubleStream.iterate(1.1, n->n+1.1);
    show("doubleStream", doubleStream.boxed());
    
    DoubleStream mapToDoubleStream = arrayList.stream().mapToDouble(x->(double)x.length());		//mapToDouble	将对象流->基本类型流
    show("mapToDoubleStream", mapToDoubleStream.boxed());	
    
    
    //产生随机数流
    DoubleStream randomDoubleStream = random.doubles();
    show("randomDoubleStream", randomDoubleStream.boxed());
    
    randomDoubleStream = random.doubles(3);					//指定stream size 3
    show("randomDoubleStream", randomDoubleStream.boxed());
    
    randomDoubleStream = random.doubles(10.0, 15.0);		//指定起点10, 边界15
    show("randomDoubleStream", randomDoubleStream.boxed());
    
    randomDoubleStream = random.doubles(8, 10.0, 15.0);		//指定stream size 8, 指定起点10, 边界15
    show("randomDoubleStream", randomDoubleStream.boxed());
    
    
    //(2)得到平均值, 和, 最大值, 最小值 
    //使用Supplier<T> 包装流
    Supplier<DoubleStream> doubleStreamSupplier = () -> DoubleStream.of(1.1,1.2,1.3,2.1,2.2);
    System.out.println("doubleStream.average()		"+doubleStreamSupplier.get().average().getAsDouble());
    System.out.println("doubleStream.sum()			"+doubleStreamSupplier.get().sum());
    System.out.println("doubleStream.max()			"+doubleStreamSupplier.get().max().getAsDouble());
    System.out.println("doubleStream.min()			"+doubleStreamSupplier.get().min().getAsDouble());
    
    DoubleSummaryStatistics doubleSummaryStatistics = doubleStreamSupplier.get().summaryStatistics();
    System.out.println("doubleStream.average()		"+doubleSummaryStatistics.getAverage());
    System.out.println("doubleStream.sum()			"+doubleSummaryStatistics.getSum());
    System.out.println("doubleStream.max()			"+doubleSummaryStatistics.getMax());
    System.out.println("doubleStream.min()			"+doubleSummaryStatistics.getMin());
    System.out.println();
    
    
    //(3) LongStream -> 数组
    double[] arrayDoubleStream = doubleStreamSupplier.get().toArray();
    System.out.println("arrayDoubleStream	"+Arrays.toString(arrayDoubleStream));
    System.out.println();
    4. 对应的,创建short,char,byte,boolean,float流
    Stream<Boolean> booleanStream = Arrays.stream(new Boolean[]{true,false,false,true});
    show("booleanStream", booleanStream);
  • 相关阅读:
    Git版本控制+配置中心(Spring Cloud Config)(八)
    NodeJS+Sidecar异构系统+Zuul(七)
    API网关Zuul(六)
    Hystrix断路器(五)
    Feign配合Ribbon和Eureka来提供负载均衡的HTTP客户端(四)
    负载均衡Ribbon(三)
    Eureka注册中心(二)
    SpringCloud介绍(一)
    Swift基础小结_2
    谓词(搜索)
  • 原文地址:https://www.cnblogs.com/xiang--liu/p/9710393.html
Copyright © 2011-2022 走看看