什么是函数式(Functional)接口java
Java从诞生日起就是一直倡导“一切皆对象”,在java里面面向对象(OOP)编程是一切。可是随着python、scala等语言的兴起和新技术的挑战,java不得不作出调整以便支持更加普遍的技术要求,也即java不但能够支持OOP还能够支持OOF(面向函数编程)python
在函数式编程语言当中,函数被当作一等公民对待。在将函数做为一等公民的编程语言中,Lambda表达式的类型是函数。可是在Java8中,有所不一样。在Java8中,Lambda表达式是对象,而不是函数,它们必须依附于一类特别的对象类型——函数式接口。es6
简单的说,在Java8中,Lambda表达式就是一个函数式接口的实例。这就是Lambda表达式和函数式接口的关系。也就是说,只要一个对象是函数式接口的实例,那么该对象就能够用Lambda表达式来表示。编程
因此之前用匿名内部类表示的如今均可以用Lambda表达式来写。app
Java 内置四大核心函数式接口dom
其它接口编程语言
package day26_1; import java.util.function.BinaryOperator; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; import org.junit.Test; /** * 函数式接口 : 只有一个抽象方法的接口, 关注的是方法的行为模式 * Consumer<T> 消费器, 消费一个T类型的对象 * void accept(T t); -> 有参无返回, 有输入没有输出 * * Supplier<T> 供给器,返回一个T类型的对象 * T get(); -> 无参有返回, 没有输入, 有输出 * * Function<T, R> 转换器, 输入一个T类型的对象, 通过某种处理返回一个R类型的对象 * R apply(T t); -> 有参有返回, 有输入, 有输出 * * Predicate<T> 判定器 , 输入一个T类型的对象, 通过某种判断后返回真或假 * boolean test(T t); -> 有参有固定的返回类型(boolean) * * BinaryOperator<T> 二元运算操做, 输入2个T类型的对象, 通过某种处理后返回一个T类型的对象 * T apply(T t1, T t2); * * 方法引用 : 适用于方法的模式同样, 有几个输入, 有没有输出。 */ public class FunctionalInterfaceTest { @Test public void test9() { Supplier<Student> supplier1 = () -> new Student(); Supplier<Student> supplier2 = Student::new; // 构造器引用 System.out.println(supplier2.get()); } @Test public void test8() { new Supplier<Double>() { @Override public Double get() { // 无参有返回 return Math.random(); } }; // lambda表达式 Supplier<Double> supplier1 = () -> Math.random(); // 方法引用 Supplier<Double> supplier2 = Math::random; System.out.println(supplier1.get()); System.out.println(supplier2.get()); } @Test public void test7() { new Consumer<String>() { @Override public void accept(String t) { System.out.println(t); } }; Consumer<String> consumer1 = t -> System.out.println(t); Consumer<String> consumer2 = System.out::println; // ::表示类或对象的方法 } @Test public void tes6() { // 获取两个学生中分数最高的。 BinaryOperator<Student> binaryOperator = (t1, t2) -> t1.getScore() > t2.getScore() ? t1 : t2; Student s1 = new Student(1, "小明", 3, 50); Student s2 = new Student(2, "小丽", 2, 80); Student apply = binaryOperator.apply(s1, s2); System.out.println(apply); } @Test public void test5() { BinaryOperator<String> binaryOperator = new BinaryOperator<String>() { @Override public String apply(String t, String u) { return t + u; } }; String apply = binaryOperator.apply("safljsfljk", "xxxxx191923"); System.out.println(apply); BinaryOperator<String> binaryOperator2 = (t1, t2) -> t1 + t2; String apply2 = binaryOperator2.apply("a324234", "我是汉字"); System.out.println(apply2); } @Test public void test4() { Predicate<String> predicate1 = new Predicate<String>() { @Override public boolean test(String t) { return t.endsWith(".java"); } }; boolean test1 = predicate1.test("hello.abc"); System.out.println(test1); // lambda表达式 Predicate<String> predicate2 = t -> t.endsWith(".java"); boolean test2 = predicate2.test("hello.java"); System.out.println(test2); } @Test public void test3() { Function<Double, Integer> function1 = new Function<Double, Integer>() { @Override public Integer apply(Double t) { return t.intValue(); } }; Integer num1 = function1.apply(3.94159); System.out.println(num1); // lambda 表达 Function<Double, Integer> function2 = t -> t.intValue(); Integer num2 = function2.apply(9.2324); System.out.println(num2); } @Test public void test2() { Supplier<Integer> supplier1 = new Supplier<Integer>() { @Override public Integer get() { return 200; } }; Integer integer = supplier1.get(); System.out.println(integer); Supplier<Integer> supplier2 = () -> 200; Integer integer2 = supplier2.get(); System.out.println(integer2); } @Test public void test1() { Consumer<String> consumer = new Consumer<String>() { @Override public void accept(String t) { System.out.println(t); } }; consumer.accept("afljaslfjalksjf"); Consumer<Integer> consumer2 = t -> System.out.println(t); consumer2.accept(234238); // 消费器 } } package day26_1; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; import org.junit.Test; public class FunctionalInterfaceExer { @Test public void exer4() { // 写一个断定器, 输入一个学生对象, 断定他是否及格了。 Predicate<Student> predicate1 = new Predicate<Student>() { @Override public boolean test(Student t) { return t.getScore() >= 60; } }; Student student = new Student(1, "小明", 5, 1.5); boolean test = predicate1.test(student); System.out.println(test); // Lambda表达式 Predicate<Student> predicate2 = t -> t.getScore() >= 60; System.out.println(predicate2.test(student)); } @Test public void exer3() { // 写一个转换器, 把学生对象转换成一个Double Function<Student, Double> fun1 = new Function<Student, Double>() { @Override public Double apply(Student t) { return t.getScore(); } }; Student student = new Student(1, "小明", 5, 80.5); Double apply = fun1.apply(student); System.out.println(apply); // lambda表达式 Function<Student, Double> fun2 = t -> t.getScore(); Double apply2 = fun2.apply(student); System.out.println(apply2); } // 作一个练习 : 使用供给器获取一个Student对象, 再使用消费型器这个对象打印输出。 @Test public void exer2() { Supplier<Student> supplier1 = new Supplier<Student>() { @Override public Student get() { return new Student(); } }; Student student = supplier1.get(); Consumer<Student> consumer1 = new Consumer<Student>() { @Override public void accept(Student t) { System.out.println(t); } }; consumer1.accept(student); // lambda表达式 Supplier<Student> supplier2 = () -> new Student(); Student student2 = supplier2.get(); Consumer<Student> consumer2 = t -> System.out.println(t); consumer2.accept(student2); } @Test public void exer1() { // 作一个供给器, 每调用一次获取一个随机100之内的整数。 Supplier<Integer> supplier1 = new Supplier<Integer>() { @Override public Integer get() { return (int)(Math.random() * 100); } }; Integer num = supplier1.get(); System.out.println(num); // lambda表达式 Supplier<Integer> supplier2 = () -> (int)(Math.random() * 100); Integer num2 = supplier2.get(); System.out.println(num2); } }