您的位置 首页 java

面试不知道JDK1.8函数式接口@FunctionalInterface被鄙视了

面试不知道JDK1.8函数式接口@FunctionalInterface被鄙视了

从我们自定义的IHello示例来看,Lambda表达式其实是一种接口类型的数据类型,严格的说Lambda表达式的数据类型是:函数式接口,是一种特殊的接口,该接口使用@FunctionalInterface注解来标记(不是必须的,可以不用该注解标记,IHello接口就没有使用该注解标记, ),并且接口中只能有一个抽象方法,可以有多个静态方法或者默认方法, 每一个该类型的lambda表达式都会被匹配到这个抽象方法。

 @FunctionalInterface
public interface Comparator<T> {
    int compare(T o1, T o2);
    // 其它 static 、default方法
}
  

@FunctionalInterface: 该注解没啥太大含义,该注解是给编译器做检查使用的,如果使用了该注解,编译器就会检查该接口中的抽象方法是不是只有一个,如果有多个就会报错:在接口Xxx中找到多个非覆盖抽象方法

 @Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface {}
  

我们完善一下我们的IHello, 使用@FunctionalInterface注解

 @FunctionalInterface
public interface IHello {
    void sayHello(String name);
}
  

我们可以将lambda表达式当作任意只包含一个抽象方法的接口类型,也就是说我们的IHello接口无论叫什么名字,接口中的方法无论叫什么名字都无所谓(只是可读性更好些),因此可以再进行抽象化一下,JDK1.8中提供了这样的函数式接口,我们也不需要再定义IHello接口了,JDK1.8中提供了Supplier、 Consumer Function 、BiFunction,这几个是比较常用的

Supplier< T > 供应商:没有参数,有返回值

 @FunctionalInterface
public interface Supplier<T> {
    T get();
}
  

Consumer< T > 消费者: 只有一个参数,没有返回值

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

Function< T, R > 函数:一个参数,一个返回值

 @FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
}
  

BiFunction< T, U, R > 二元函数:两个参数,一个返回值

 @FunctionalInterface
public interface BiFunction<T, U, R> {
    R apply(T t, U u);
}
  

Comparator< T > 比较器:接收两个参数,返回比较的结果

 @FunctionalInterface
public interface Comparator<T> {
    int compare(T o1, T o2);
}
  

使用以上四大函数式接口来取代自定义的接口IHello

 public class Main {
    private static String end = ".";

    public static void main(String[] args) {
        // 直接使用JDK1.8提供的接口,不需要再定义IHello接口, 直接使用JDK提供的接口来接收Lambda表达式
        Supplier<String> supplier = () -> "mengday: happy new year everyone!";
        String result = supplier.get();
        System.out.println(result);

        Consumer<String> consumer = (name) -> System.out.println(name + ": " + "happy new year everyone!");
        consumer.accept("mengday");

        Function<String, String> func = (name) -> name + ": " + "happy new year everyone!";
        String hi = func.apply("mengday");
        System.out.println(hi);


        // 在代码块的内部可以访问静态全局变量
        // 在代码块中可以访问外边局部变量
        // 在代码块的内部可以修改全局静态变量
        // 在代码块内部是不能访问接口中的其它方法的
        String split = ": ";
        BiFunction<String, String, String> biFunction = (String name, String msg) -> {
            end = "!";
            String hello = name + split + msg + end;
            return hello;
        };
        String hello = biFunction.apply("mengday", "happy new year everyone");
        System.out.println(hello);

        // 根据字符串长度比较大小
        Comparator<String> comparator = (s1, s2) -> s1.length() - s2.length();
        int compare = comparator.compare("abc", "ab");
        System.out.println(compare);
    }
}
  

Predicate< T > 断言 谓词 : 用于测试一个条件的真假

 package java.util.function;
import java.util. Object s;

@FunctionalInterface
public interface Predicate<T> {
    // 在给定的参数上评估这个谓词
     boolean  test(T t);

    // 返回一个组合的谓词,表示该谓词与另一个谓词的短路逻辑AND
    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

    // 返回表示此谓词的逻辑否定的谓词,相当于not
    default Predicate<T> negate() {
        return (t) -> !test(t);
    }

    // 返回一个组合的谓词,表示该谓词与另一个谓词的短路逻辑或
    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    // 返回根据 Objects.equals(Object, Object)测试两个参数是否相等的谓词
    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef) ? Objects::isNull : object -> targetRef.equals(object);
    }
}
  

Main

 public static void main(String[] args) {
    // 可以构造复杂的条件: 并且and、或者or、否negate
    String email = "mengday@gmal.com";
    Predicate<String> predicate = (str) -> str.length() > 20;

    // 测试 emial.length > 0 的boolean
    boolean result = predicate.test(email);     // false

    // 测试 !(emial.length > 0) 的boolean
    result = predicate.negate().test(email);    // true

    Predicate<String> orPredicate = (str) -> str.contains("@");
    // 测试 emial.length > 0 or emial.contains("@")
    result = predicate.or(orPredicate).test(email);     // true
}  

文章来源:智云一二三科技

文章标题:面试不知道JDK1.8函数式接口@FunctionalInterface被鄙视了

文章地址:https://www.zhihuclub.com/187983.shtml

关于作者: 智云科技

热门文章

发表回复

您的电子邮箱地址不会被公开。

网站地图