0
点赞
收藏
分享

微信扫一扫

JDK1.8新特性

花海书香 2022-02-10 阅读 185

Stream

package _04_Stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Stream对象创建
 */
public class Stream_01 {
    public static void main(String[] args) {
        //1数组 通过stream类的of方法即可
        String[] strings = {"a","b","c","d"};
        Stream<String> stream1 = Stream.of(strings);
        //2集合 通过集合对象的stream方法即可
        List<String> strings2 = Arrays.asList(strings);
        Stream<String> stream2 = strings2.stream();

        //3 通过Stream的generate方法创建无限流
        //该流无限大 建议使用limit限制条数
        //参数是一个Supplier接口 有一个get方法 无惨 有返回值
        //放回的数据 就会被放到这个无限流中 也就是目前这个流中的数据都是1
        Stream<Integer> stream3 = Stream.generate(()->1);
        stream3.limit(10).forEach(x-> System.out.println(x));

        //4通过Steam。iterate方法创建无限流
        // 第一个参数是起始值,第二个参数 是一个UnaryOperator,是function的子类,所以是有参有返回值
        // x->x+2 : 等于步长为2, 那么此时 数据流中的内容为 1,3,5,7,9,11.....
        Stream<Integer> stream4 = Stream.iterate(1,x->x+2);
        stream4.limit(5).forEach(x-> System.out.println(x));

        //5以有类的streamAPI
        String string = "asdf";
        IntStream is = string.chars();
        is.forEach(x-> System.out.println(x));
    }
}

方法引用和构造器调用

package _03_FunCall;

import java.util.function.Supplier;

/**
 * 对象引用::成员方法
 */
public class FunCall_01 {
    public static void main(String[] args) {
        Integer i1 = 10;
        //方法调用
        String str = i1.toString();
        System.out.println(str);
        //lambda表达式写法
        Supplier<String > su = ()->i1.toString();
        System.out.println(su.get());
        //方法引用写法
        Supplier<String> su1 = i1::toString;
        System.out.println(su1.get());
    }
}
package _03_FunCall;

import java.util.function.BiFunction;

/**
 * ;类名::静态方法名
 */
public class FunCall_02 {
    public static void main(String[] args) {
    int max = Integer.max(10,33);
        BiFunction<Integer,Integer,Integer> bf = Integer::max;
        System.out.println(bf.apply(10,33));

    }
}
package _03_FunCall;


import java.util.function.BiPredicate;

/**
 * 类名::成员方法
 */
public class FunCall_03 {
    public static void main(String[] args) {
        BiPredicate<String,String> bp = String::equals;
        System.out.println(bp.test("asd","asd"));
    }
}
package _03_FunCall;

import java.util.function.Supplier;

/**
 * 创建对象
 */
public class FunCall_04 {
    public static void main(String[] args) {
        Object o1 = new Object();
        Supplier<Object> sp = Object::new;
        System.out.println(sp.get());
    }

}
package _03_FunCall;

import java.util.function.BiFunction;
import java.util.function.Function;

/**
 *
 */
public class FunCall_05 {
    public static void main(String[] args) {
        Integer[] is = new Integer[10];
        Function<Integer,Integer[]> fun = Integer[]::new;
        is = fun.apply(5);
        System.out.println(is.length);
        is[0] = 100;
        BiFunction<Integer[],Integer,Integer> bc = (arr,index)->arr[index];
        System.out.println(bc.apply(is,0));
    }
}

中间操作

package _04_Stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 中间操作 又称为转换算子,对数据源进行处理
 *
 * 一个流可以有N个中间操作,每一个中间操作都会返回一个新的流,方便下一个操作使用,可以做到链式调用
 *
 * 但是一个流只能有一个终止操作
 *
 * 如果不执行终止操作,中间操作也不会执行
 *
 * filter : 对元素进行筛选,不符合条件的就不要了
 *
 * distinct : 去掉重复的元素
 *
 * limit : 取一个数据集合的前几条数据
 *
 * skip : 跳过多少元素
 *
 * sorted : 排序
 *
 * map : 在遍历集合的过程中对数据进行操作
 */
public class Stream_02 {
    public static void main(String[] args) {
        List<String> strings = Arrays.asList("a", "b", "c", "d", "a");
        /**
         * filter : 对元素进行过滤,不符合条件的就不要了
         */
        Stream<String> stream = strings.stream();
        // collect : 收集器,把流转换为集合
        // x -> !x.equals("a") 只要不是a的
        List<String> result = stream.filter(x -> !x.equals("a")).collect(
                Collectors.toList());
        System.out.println(result);
        // 流一旦使用过,必须重新生成,否则报错,所以每个中间操作都是返回一个新的流,因为原来的流就不能用了
        // java.lang.IllegalStateException: stream has already been operated
        // upon or closed
        // stream.forEach(x->System.out.println(x));

        /**
         * distinct 去除重复
         */
        stream = strings.stream();
        result = stream.distinct().collect(Collectors.toList());
        System.out.println(result);
        /**
         * map
         */
        List<Integer> says = Arrays.asList(2000, 1000, 2800, 6666, 8888);
        Stream<Integer> stream1 = says.stream();
        // 所有人涨薪百分之十
        says = stream1.map(x -> x + x / 100 * 10).collect(Collectors.toList());
        System.out.println(says);

        /**
         * sorted 排序
         */
        stream1 = says.stream();
        // 默认是升序 [1100, 2200, 3080, 7326, 9768]
        // says = stream1.sorted().collect(Collectors.toList());
        // 降序 [9768, 7326, 3080, 2200, 1100]
        says = stream1.sorted((x, y) -> y - x).collect(Collectors.toList());
        System.out.println(says);
    }
}
举报

相关推荐

0 条评论