Java8新特性之函数式接口

函数式接口

1、Supplier

​ 无参有返回值的接口。Supplier接口是对象实例的提供者,定义了一个名叫get的抽象方法,它没有任何入参,并返回一个泛型T对象。

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

2、Consumer

​ 有参无返回值的接口。Consumer接口是一个类似消费者的接口,定义了一个名叫accept的抽象方法,它的入参是一个泛型T对象,没有任何返回。

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

3、Function

​ 有参有返回值的接口,Function接口是根据一个类型的数据得到另一个类型的数据,前者称为前置条件,后者称为后置条件。有参数有返回值。

@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);

4、Predicate

​ 有参,返回值为Boolean的接口。Predicate接口是判断是与否的接口,定义了一个名叫test的抽象方法,它的入参是一个泛型T对象,并返回一个boolean类型。

public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);

理解:

函数式接口:只有一个方法的接口

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}
//超级多FunctionalInterface
//简化编程模型,在新版本的框架底层大量应用
//foreach(消费者类型的函数式接口)

Function 函数式接口

package com.example.juc.function;

import java.util.function.Function;

/**
 * @author swh
 * @date 2022/7/28 14:19
 */

/*
* Function函数型接口
* 有一个输入参数,有一个输出
* (只要是函数式接口,都可以用lambda表达式简化)
* */
public class Demo01 {
    public static void main(String[] args) {
        //
       /* Function function = new Function<String,String>() {
            @Override
            public String apply(String s) {
                return s;
            }
        };*/
        Function<String,String> function =(s)->{return s;};
    }
}

Predicate 断定型接口

package com.example.juc.function;

import java.util.function.Predicate;

/**
 * @author swh
 * @date 2022/7/28 14:29
 */
/*
* 断定型接口
* 有一个输入参数,返回值只能是布尔值
* */
public class Demo02 {
    public static void main(String[] args) {
        //判断字符串是否为空
      /*  Predicate<String> predicate = new Predicate<String>() {
            @Override
            public boolean test(String str) {
                return str.isEmpty();
            }
        };*/
        Predicate<String> predicate =str->{ return str.isEmpty();};
        System.out.println(predicate.test("s"));
    }
}

Consumer 消费型接口

package com.example.juc.function;

import java.util.function.Consumer;

/**
 * @author swh
 * @date 2022/7/28 14:39
 */

/*
 * 消费型接口:只有输入,没有返回值
 * */
public class Demo03 {
    public static void main(String[] args) {
     /*   Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };*/
        Consumer<String> consumer=s->{
            System.out.println(s);
        };
        consumer.accept("ss");
    }
}

Supplier 供给型接口

package com.example.juc.function;

import java.util.function.Supplier;

/**
 * @author swh
 * @date 2022/7/28 14:45
 */
/*
* 供给型接口
* 没有参数,只有返回值
* */
public class Demo04 {
    public static void main(String[] args) {
       /* Supplier supplier = new Supplier<String>() {
            @Override
            public String get() {
                return "123";
            }
        };*/
        Supplier supplier =()->{return "123";};
        System.out.println(supplier.get());
    }
}

任何一个新的技术,绝对不是仅仅只是覆盖了原来的技术,优势和补充!

Lock 接口

公平锁: 十分公平;可以先来后到

非公平锁: 十分不公平;可以插队(默认非公平锁)

Callable:
与Runnable的不同

  1. 可以有返回值
  2. 可以抛出异常
  3. 方法不同,run() | call()
  4. 有缓存
  5. 结果可能需要等待,会阻塞!
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShanHai山海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值