美文网首页
Java-8-IntStream接口

Java-8-IntStream接口

作者: Cool_Pomelo | 来源:发表于2020-02-06 11:04 被阅读0次

    Java-8-IntStream接口

    interface IntStream extends BaseStream<Integer, IntStream>

    下面这段代码计算菜单的热量:

    
    
    int calories = menu.stream()
    .map(Dish::getCalories)
    .reduce(0, Integer::sum);
    
    
    

    这段代码有一个问题,它有一个暗含的装箱成本。每个 Integer 都必须拆箱成一个原始类型,再进行求和

    要是可以直接像下面这样调用 sum 方法,岂不是很好:

    
    int calories = menu.stream()
    .map(Dish::getCalories)
    .sum();
    
    

    但这是不可能的。问题在于 map 方法会生成一个 Stream<T> 。虽然流中的元素是 Integer 类型,但 Streams 接口没有定义 sum 方法

    Java 8引入了三个原始类型特化流接口来解决这个问题: IntStream 、 DoubleStream 和LongStream ,分别将流中的元素特化为 int 、 long 和 double ,从而避免了暗含的装箱成本。每个接口都带来了进行常用数值归约的新方法,比如对数值流求和的 sum ,找到最大元素的 max 。此外还有在必要时再把它们转换回对象流的方法

    生成流

    • IntStream of(int t)
    • IntStream of(int... values)

    • IntStream iterate(final int seed, final IntUnaryOperator f)

    • IntStream generate(IntSupplier s)

    使用
    
    public class M1 {
    
    
        public static void main(String[] args) {
    
            IntStream intStream1 = IntStream.of(1,2,3,4,5,6,7,8,9);
    
            intStream1.forEach(System.out::println);
    
            System.out.println("***********************************************");
            int[] d = {1,2,3,4,5,6,7,8,9};
    
            IntStream intStream2 = IntStream.of(d);
    
            intStream2.forEach(System.out::println);
    
            System.out.println("***********************************************");
    
            IntStream intStream3 = IntStream.iterate(1,x->x+3).limit(10);
    
            intStream3.forEach(System.out::println);
    
            System.out.println("***********************************************");
    
            IntStream intStream4 = IntStream.generate(()->100).limit(10);
    
            intStream4.forEach(System.out::println);
            
    
        }
    }
    
    
    

    转换流

    • asLongStream
    • asDoubleStream

    • toArray

    使用
    
    
    public class M2 {
    
        public static void main(String[] args) {
    
            IntStream intStream1 = IntStream.of(1,2,3,4,5,6,7,8,9);
    
            LongStream longStream1 = intStream1.asLongStream();
    
            longStream1.forEach(System.out::println);
    
            System.out.println("***********************************************");
    
            DoubleStream doubleStream1 = intStream1.asDoubleStream();
            
            doubleStream1.forEach(System.out::println);
    
              System.out.println("***********************************************");
    
            int[] d = IntStream.of(1,2,3,4,5,6,7,8,9).toArray();
    
            for (int i : d){
                System.out.println(i);
            }
            
        }
    }
    
    
    
    
    

    数据汇总

    
    // * and is equivalent to:
    // * <pre>{@code
    // *     return reduce(0, Integer::sum);
    // * }</pre>
        int sum();
    
        //  * and is equivalent to:
        //  * <pre>{@code
        //  *     return reduce(Integer::min);
        //  * }</pre>
        OptionalInt min();
    
    
    //    * and is equivalent to:
    //      * <pre>{@code
    //      *     return reduce(Integer::max);
    //      * }</pre>
        OptionalInt max();
    
    
    //    * equivalent to:
    //      * <pre>{@code
    //      *     return mapToLong(e -> 1L).sum();
    //      * }</pre>
        long count();
    
    
        OptionalDouble average();
    
    
        IntSummaryStatistics summaryStatistics();
    
    
    
    使用
    public class M1 {
    
        public static void main(String[] args) {
    
    
            List<Employee> employees = Employee.supply_Ems();
    
            IntStream intStream1 = IntStream.of(1,2,3,4,5,6,7,8,9);
    
            int sum = intStream1.sum();
    
            System.out.println(sum);
    
            System.out.println("---------------------------------------");
    
            //统计员工工资
            IntStream intStream2 = employees.stream()
                                .mapToInt(Employee::getSalary);
    
    //        intStream2.forEach(System.out::println);
    
            //统计工资总数
    //        int sum1  = intStream2.sum();
    
    //        System.out.println(sum1);
    
            System.out.println("---------------------");
    // 找出最低工资
    //        OptionalInt optionalInt = intStream2.min();
    //
    //        System.out.println(optionalInt.getAsInt());
    
            System.out.println("---------------------");
    
    
            // 找出最高工资
    
    //        OptionalInt optionalInt2 = intStream2.max();
    //
    //        System.out.println(optionalInt2.getAsInt());
    
    
    //        long num = intStream2.count();
    //// 数据个数
    //        System.out.println(num);
    
            System.out.println("---------------------");
    
    
            // 平均工资
    //        OptionalDouble d1 = intStream2.average();
    //
    //        System.out.println(d1.getAsDouble());
    
            System.out.println("---------------------");
    
            IntSummaryStatistics intSummaryStatistics = intStream2.summaryStatistics();
    
            System.out.println(intSummaryStatistics.toString());
    
            /*
            IntSummaryStatistics{count=30, sum=173068, min=1621, average=5768.933333, max=9000}
    
             */
            
        }
    }
    
    
    

    range && rangeClosed

    
        // * @apiNote
        //  * <p>An equivalent sequence of increasing values can be produced
        //  * sequentially using a {@code for} loop as follows:
        //  * <pre>{@code
        //  *     for (int i = startInclusive; i < endExclusive ; i++) { ... }
        //  * }</pre>
    static IntStream range(int startInclusive, int endExclusive) 
    
    
    
    //  * @apiNote
    //      * <p>An equivalent sequence of increasing values can be produced
    //      * sequentially using a {@code for} loop as follows:
    //      * <pre>{@code
    //      *     for (int i = startInclusive; i <= endInclusive ; i++) { ... }
    //      * }</pre>
    static IntStream rangeClosed(int startInclusive, int endInclusive)
    
    使用
    
    public class M1 {
    
    
        public static void main(String[] args) {
    
    
    //        IntStream.range(1,100).forEach(System.out::println);
    
            System.out.println("----------------------------------");
    
    //        IntStream.rangeClosed(1,100).forEach(System.out::println);
    
            /***************************************/
    
             int[] a1 = IntStream.rangeClosed(1,10)
                    .map(i -> i + i)
                    .toArray();
    
            System.out.println(Arrays.toString(a1));
            
        }
    }
    
    
    
    

    boxed

    如果想转换回Integer流,可以使用这个方法

    
    public class M3 {
    
        public static void main(String[] args) {
    
            int[] d1 = Data.supply();
    
            IntStream i1 = IntStream.of(d1);
    
            Stream<Integer> integerStream = i1.boxed();
    
            integerStream.sorted(Integer::compareTo).forEach(System.out::println);
    
    
    
        }
    }
    
    

    其它

    
    public class M2 {
    
        public static void main(String[] args) {
    
            IntStream i1 = IntStream.of(1,1,1,2,5,8,9,55,5,5,5,8);
    
            i1.distinct().forEach(System.out::println);
    
            System.out.println("-----------------------------------");
    
            int[] a1 = Data.supply();
    
            System.out.println(Arrays.toString(a1));
    
            System.out.println("-----------------------------------");
    
            int[] sort =  IntStream.of(a1).sorted().toArray();
    
            System.out.println(Arrays.toString(sort));
    
    
    
        }
    }
    
    
    

    相关文章

      网友评论

          本文标题:Java-8-IntStream接口

          本文链接:https://www.haomeiwen.com/subject/uvpyxhtx.html