什麼是函數式(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); } }