JUC-函数式接口/Stream流

四大函数式接口(Consumer、Function、Predicate、Supplier)

新技术:lambda表达式、链式编程、函数式接口、Stream流式计算

函数式接口:只有一个方法的接口,在新版本的框架底层大量应用

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}

foreach(消费者类的函数式接口)

Function

一个输入参数,一个输出

@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);//返回R类型
}

测试:(函数型接口,可以用lambda表达式简化)

public static void main(String[] args) {
        //匿名内部类
        Function<String, String> stringStringFunction = new Function<String, String>(){
            @Override
            public String apply(String str) {
                return str;
            }
        };
        //lambda表达式写法
        Function<String, String> function = (str)->{return str;};
        System.out.println(stringStringFunction.apply("1"));
        System.out.println(function.apply("lambda"));
    }
Predicate断定型接口

一个输入参数,返回值只能是boolean

@FunctionalInterface
public interface Predicate<T> {
    boolean test(T t);//返回boolean
}

测试:

public static void main(String[] args) {
    Predicate<String> predicate = new Predicate<String>() {
        @Override
        public boolean test(String str) {
            return str.isEmpty();
        }
    };
    //Predicate<String> predicateLambda = (str)->{return str.isEmpty()};
    //lambda表达式写法
    Predicate<String> predicateLambda = String::isEmpty;
    System.out.println(predicate.test("a"));
    System.out.println(predicateLambda.test("A"));

}
Consumer消费型接口

一个输入参数,没有返回值

@FunctionalInterface
public interface Consumer<T> {
    void accept(T t);//
}

测试:

public static void main(String[] args) {
    Consumer<String> stringConsumer = new Consumer<String>() {
        @Override
        public void accept(String s) {
            System.out.println(s);
        }
    };
    //        Consumer<String> stringConsumerLambda = (str)->{
    //            System.out.println(str);
    //        }
    //lambda表达式写法
    Consumer<String> stringConsumerLambda = System.out::println;
    stringConsumer.accept("hello");
    stringConsumerLambda.accept("hello");
}
Supplier供给型接口

没有参数,只有返回值

@FunctionalInterface
public interface Supplier<T> {
    T get();//返回T类型
}

测试:

public static void main(String[] args) {
    Supplier<Integer> supplier = new Supplier<Integer>() {
        @Override
        public Integer get() {
            return 1024;
        }
    };
    //lambda表达式写法
    Supplier<Integer> supplierLambda = ()->{return 1024;};
    System.out.println(supplier.get());
    System.out.println(supplierLambda.get());
}
Stream流式计算

lambda+函数式接口+链式编程+stream流

//id偶数、年龄大于23、用户名转换为大写、用户名倒着排序、只输出一个用户---一行代码完成

public static void main(String[] args) {
    User a = new User(1, "a", 21);
    User b = new User(2, "b", 22);
    User c = new User(3, "c", 23);
    User d = new User(4, "d", 24);
    User e = new User(6, "e", 25);
    // 集合就是存储
    List<User> users = Arrays.asList(a, b, c, d, e);
    //计算交给流
    users.stream()
        .filter(u -> {return u.getId()%2==0;})//id是偶数
        .filter(u -> {return u.getAge()>23; })//年龄大于23
        .map(u -> {return u.getName().toUpperCase();})//转换为大写
        .sorted((uu1,uu2)->{return uu2.compareTo(uu1);})//排序
        .limit(1)//限制数量
        .forEach(System.out::println);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值