JDK1.8新特性函数式接口

概述

对于只有一个抽象方法的接口,需要这种接口的对象,就可以提供一个lambda表达式。这种接口称为函数式接口(functional interface)。

在定义函数式接口的时候可以加入@FunctionalInterface注解来修饰该接口

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

JDK1.8常用函数式接口

函数式接口 参数类型 返回类型 抽象方法名 描述 其他方法
Rnnnable void run 作为无参数或者返回值的动作运行
Supplier<T> T get 提供一个T类型的值
Consumer<T> T void accept 处理一个T类型的数据 andThen
BiConsumer<T, U> T ,U void accept 处理T类型和U类型的数据 andThen
Function<T, R> T R apply 有一个T类型的参数,返回R类型的值 compse, andThen, identity
BiFunction<T,U,R> T,U R apply 一个T类型参数,和U类型的参数,返回R类型值 andThen
Perdicate<T> T boolean test 布尔值函数 and,or,negate,isEqual

Supplier<T>接口

函数抽象方法无参数,返回一个T类型的值

/**
 * @author justLym
 * @version 1.0.0 2020/3/11 19:54
 **/
public class SupplierDemo {
    public static void main(String[] args) {
        test(() -> "justlym");
    }

    public static void test(Supplier<String> supplier) {
        System.out.println(supplier.get());
    }
}

Consumer<T>接口

/**
 * @author justLym
 * @version 1.0.0 2020/3/11 19:57
 **/
public class ConsumerDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        
        list.add("justLym");
        
        list.forEach((item) -> System.out.println(item));
        
        test("justLym", s -> {s += "1";}, s -> {s += "2";});
    }
    
    public static void test(String string, Consumer<String> consumer , Consumer<String> consumer2) {
        consumer.andThen(consumer2).accept(string);
    }
}

Perdicate<T>接口

抽象方法返回布尔值,用于方法执行过程中判断使用

public class PredicateDemo {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        list.add("justLym");

        list.add("1234");

        list.removeIf((item) -> item.startsWith("justLym"));

        list.forEach(System.out::print);
    }
}

Predicate<T>接口其他方法
  • Predicate<T> and(Predicate<? super T> other) 返回一个Predicate对象,两个函数式接口组合结果取结果的与,相当于逻辑判断的&&
  • Predicate<T> negate() 返回一个Predicate对象该函数接口的test方法执行结果取反,相当于逻辑判断的!
  • Predicate<T> or(Predicate<? super T> other) 返回一个Predicate对象,两个函数式接口组合结果取或,相当去逻辑判断的||
  • static <T> Predicate<T> isEqual(Object targetRef)返回一个Predicate对象, 这是一个静态方法,相当于Objects.equals(Object, Object).
/**
 * @author justLym
 * @version 1.0.0 2020/3/11 20:09
 **/
public class PredicateDemo {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        list.add("justLym");

        list.add("1234");

        Predicate<String> justLym = Predicate.isEqual("justLym");
        list.removeIf(justLym);

        list.forEach(System.out::print);
    }
}

Function<T,R>接口

/**
 * @author justLym
 * @version 1.0.0 2020/3/11 20:34
 **/
public class FunctionDemo {

    public static void main(String[] args) {
        Integer test = test("1", Integer::parseInt);

        System.out.println(test);
    }

    /**
     * 将String类型转成Integer类型
     * @param string
     * @param function
     * @return
     */
    public static Integer test(String string, Function<String, Integer> function) {
        return function.apply(string);
    }

    /**
     * 将String类型转成Integer类型,再将Integer转成String类型
     * @param string
     * @param function
     * @param function1
     * @return
     */
    public static String test(String string, Function<String, Integer> function, Function<Integer, String> function1) {
        return function.andThen(function1).apply(string);
    }
}
public static void main(String[] args) {
        Function<Integer, Integer> times2 = i -> i*2;
        Function<Integer, Integer> squared = i -> i*i;
        
        System.out.println(times2.apply(4));
        
        System.out.println(squared.apply(4));
        
		//32                先4×4然后16×2,先执行apply(4),在times2的apply(16),先执行参数,再执行调用者。
        System.out.println(times2.compose(squared).apply(4));  
        
        //64               先4×2,然后8×8,先执行times2的函数,在执行squared的函数。
        System.out.println(times2.andThen(squared).apply(4));  
        
		 //16
        System.out.println(Function.identity().compose(squared).apply(4));  
    }
发布了19 篇原创文章 · 获赞 18 · 访问量 5259

猜你喜欢

转载自blog.csdn.net/justLym/article/details/104803830