package com.java.design.java8; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.function.*; import java.util.stream.Collectors; /** * @author 陳楊 */ @RunWith(SpringRunner.class) @SpringBootTest public class FuncInterface {
// 函數式編程的理解 // // 函數接口式編程 是 對 業務應用的進一步抽象 // 在類方法定義中 只需實現FunctionalInterface 而無論業務實現的邏輯 // 在外部應用 調用該業務時 使用Lambda表達式 靈活實現其業務邏輯
一、Function接口java
// Function Function<Integer, Integer> sum = integer -> integer + 1; Function<Integer, Integer> multiply = integer -> integer * integer; List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); public int testFunctionCompose(Integer integer) { return sum.compose(multiply).apply(integer); } public int testFunctionAndThen(Integer integer) { return sum.andThen(multiply).apply(integer); }
二、BiFunction接口spring
// BiFunction BiFunction<Integer, Integer, Integer> subtract = (first, last) -> first - last; public int testBiFunctionAndThen(Integer first, Integer last) { return subtract.andThen(multiply).apply(first, last); }
三、BinaryOperator接口編程
// BinaryOperator BinaryOperator<Integer> binaryOperator = (first, last) -> first - last; public int testBinaryOperator(Integer first, Integer last) { return binaryOperator.apply(first, last); } public String testMinBy(String first, String last, Comparator<String> comparator) { return BinaryOperator.minBy(comparator).apply(first, last); } public String testMaxBy(String first, String last, Comparator<String> comparator) { return BinaryOperator.maxBy(comparator).apply(first, last); }
//比較器 //比較字符串的長度 Comparator<String> length = (first, last) -> first.length() - last.length(); //比較字符串首字母ASCII碼大小 Comparator<String> asc = (first, last) -> first.charAt(0) - last.charAt(0);
四、Predicate接口app
// Predicate public List<Integer> testPredicate(Predicate<Integer> predicate) { return list.stream().filter(predicate).collect(Collectors.toList()); } public Predicate<String> isEqual(Object object) { return Predicate.isEqual(object); } public Predicate<Integer> notPredicate(Predicate<Integer> predicate) { return Predicate.not(predicate); } public List<Integer> testPredicateNegate(Predicate<Integer> predicate) { return list.stream().filter(predicate.negate()).collect(Collectors.toList()); } public List<Integer> testPredicateAnd(Predicate<Integer> first, Predicate<Integer> last) { return list.stream().filter(first.and(last)).collect(Collectors.toList()); } public List<Integer> testPredicateOr(Predicate<Integer> first, Predicate<Integer> last) { return list.stream().filter(first.or(last)).collect(Collectors.toList()); }
五、Supplier接口ide
// Supplier @Data @AllArgsConstructor @NoArgsConstructor private class Student { private Integer id; private String name; private String sex; private Integer age; private String addr; private Double salary; }
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.2.RELEASE) 2019-01-31 11:51:58.460 INFO 12080 --- [ main] com.java.design.java8.FuncInterface : Starting FuncInterface on DESKTOP-87RMBG4 with PID 12080 (started by 46250 in E:\IdeaProjects\design) 2019-01-31 11:51:58.461 INFO 12080 --- [ main] com.java.design.java8.FuncInterface : No active profile set, falling back to default profiles: default 2019-01-31 11:51:58.988 INFO 12080 --- [ main] com.java.design.java8.FuncInterface : Started FuncInterface in 0.729 seconds (JVM running for 1.556) --------------------Function接口的理解--------------------- 65 81 ------------------BiFunction接口的理解--------------------- 64 -------------------Predicate接口的理解--------------------- 獲取知足條件的集合:大於4 [5, 6, 7, 8, 9] ------------------------------ 獲取知足條件的集合:大於4且是偶數 [6, 8] ------------------------------ 獲取知足條件的集合:大於4 取反 [1, 2, 3, 4, 0] ------------------------------ 獲取知足條件的集合:大於4或是偶數 [2, 4, 5, 6, 7, 8, 9, 0] ------------------------------ 使用Objects的Equals方法判斷對象是否相同 true ------------------------------ Predicate.not()返回(Predicate<T>)target.negate(); [1, 2, 3, 4, 0] ------------------------------ 雙重否認表確定 [5, 6, 7, 8, 9] ------------------------------ -------------------Supplier接口的理解--------------------- FuncInterface.Student(id=1, name=Kirito, sex=Male, age=18, addr=ShenZhen, salary=9.99999999E8) ------------------------------ ---------------BinaryOperator接口的理解------------------- 繼承BiFunction的Apply方法 實現減法 10 - 2 = 8 ------------------------------ 字符串較短的是:Asuna 字符串較長的是:Kirito ------------------------------ 字符串首字母ASCII碼較小的是:Asuna 字符串首字母ASCII碼較大的是:Kirito Process finished with exit code 0
一、Function接口函數式編程
@Test public void testFuncInterface() { System.out.println("--------------------Function接口的理解---------------------\n"); // Function接口的理解 // public interface Function<T, R> // R apply(T t); // Represents a function that accepts one argument and produces a result. // 一個函數:接收一個參數 返回一個結果 // T 輸入類型 R 輸出類型 /*default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } * Returns a composed function that first applies the {@code before} * function to its input, and then applies this function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. */ // 輸入-->beforeFunction()處理-->獲得結果做爲下一個函數apply()的輸入參數 造成函數式接口的串聯調用 // beforeFunction 在當前函數apply前 進行調用 /*default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } * Returns a composed function that first applies this function to * its input, and then applies the {@code after} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. */ // 輸入-->apply()處理-->獲得結果做爲下一個函數after.apply()的輸入參數 造成函數式接口的串聯調用 // afterFunction 在當前函數apply後 進行調用 /*static <T> Function<T, T> identity() { return t -> t; * Returns a function that always returns its input argument. }*/ // 老是返回輸入參數 // 總結: // function1.compose(function2) 執行順序 -->BeforeFunction()-->thisFunction()--> function2 --> function1 // function1.andThen(function2) 執行順序 -->thisFunction()-->AfterFunction()--> function1 --> function2 // 前一個函數的運算結果 做爲下一個函數的輸入參數 // 理解運行時機 能夠類比 Junit中 @Before 與 @After System.out.println(this.testFunctionCompose(8)); System.out.println(this.testFunctionAndThen(8));
二、BiFunction接口函數
System.out.println("------------------BiFunction接口的理解---------------------\n"); // BiFunction 接口 的 理解 // @FunctionalInterface // public interface BiFunction<T, U, R> { // R apply(T t, U u); // 一個函數:接收二個參數 返回一個結果 /* default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t, U u) -> after.apply(apply(t, u)); } } */ // 利用反證法 能夠證實 BiFunction 沒有 compose方法 (提示: 參數 與 返回值) // 將2個參數應用於BiFunction 只會獲得一個返回值 這個返回值會做爲Function傳入的參數 // biFunction.andthen(function) System.out.println(this.testBiFunctionAndThen(10, 2));
三、Predicate接口測試
System.out.println("-------------------Predicate接口的理解---------------------\n"); // public interface Predicate<T> { // Represents a predicate (boolean-valued function) of one argument. /* * Evaluates this predicate on the given argument. * * 接收一個判斷 判斷是否知足預期條件 返回true false * boolean test(T t); */ System.out.println("獲取知足條件的集合:大於4"); System.out.println(this.testPredicate(in -> in > 4)); System.out.println("------------------------------\n"); /* * Returns a composed predicate that represents a short-circuiting logical * AND of this predicate and another. When evaluating the composed * predicate, if this predicate is {@code false}, then the {@code other} * predicate is not evaluated. * * 短路邏輯與計算 * default Predicate<T> and(Predicate<? super T> other) { * Objects.requireNonNull(other); * return (t) -> test(t) && other.test(t); }*/ System.out.println("獲取知足條件的集合:大於4且是偶數"); System.out.println(this.testPredicateAnd(in -> in > 4, in -> in % 2 == 0)); System.out.println("------------------------------\n"); /* * Returns a predicate that represents the logical negation of this * predicate. * * 取反 * default Predicate<T> negate() { * return (t) -> !test(t); * } */ System.out.println("獲取知足條件的集合:大於4 取反"); System.out.println(this.testPredicateNegate(in -> in > 4)); System.out.println("------------------------------\n"); /* * Returns a composed predicate that represents a short-circuiting logical * OR of this predicate and another. When evaluating the composed * predicate, if this predicate is {@code true}, then the {@code other} * predicate is not evaluated. * * 短路邏輯或計算 * default Predicate<T> or(Predicate<? super T> other) { * Objects.requireNonNull(other); * return (t) -> test(t) || other.test(t); * } */ System.out.println("獲取知足條件的集合:大於4或是偶數"); System.out.println(this.testPredicateOr(in -> in > 4, in -> in % 2 == 0)); System.out.println("------------------------------\n"); /* * Returns a predicate that tests if two arguments are equal according * to {@link Objects#equals(Object, Object)}. * * 根據Objects的equals方法 來判斷兩個對象 是否相同 * static <T> Predicate<T> isEqual(Object targetRef) { * return (null == targetRef) * ? Objects::isNull * : object -> targetRef.equals(object); * } */ System.out.println("使用Objects的Equals方法判斷對象是否相同"); System.out.println(this.isEqual("Kirito").test("Kirito")); System.out.println("------------------------------\n"); /* * Returns a predicate that is the negation of the supplied predicate. * This is accomplished by returning result of the calling * {@code target.negate()}. * * 返回提供的predicate的否認 * @SuppressWarnings("unchecked") * static <T> Predicate<T> not(Predicate<? super T> target) { * Objects.requireNonNull(target); * return (Predicate<T>)target.negate(); * } * } */ System.out.println("Predicate.not()返回(Predicate<T>)target.negate(); "); System.out.println(testPredicate(this.notPredicate(integer -> integer > 4))); System.out.println("------------------------------\n"); System.out.println("雙重否認表確定"); System.out.println(testPredicateNegate(this.notPredicate(integer -> integer > 4))); System.out.println("------------------------------\n");
四、Supplier接口ui
System.out.println("-------------------Supplier接口的理解---------------------\n"); /* * Represents a supplier of results. * * public interface Supplier<T> { * T get(); * } */ // 構造方法引用 構造函數接口實例 // 利用Supplier接口 Student類必需要有無參的構造方法 // Supplier<Student> studentSupplier = () -> new Student(); Supplier<Student> studentSupplier = Student::new; Student student = studentSupplier.get(); student.setId(1); student.setName("Kirito"); student.setSex("Male"); student.setAge(18); student.setSalary(999999999.0); student.setAddr("ShenZhen"); System.out.println(student); System.out.println("------------------------------\n");
五、BinaryOperator接口this
System.out.println("---------------BinaryOperator接口的理解-------------------\n"); /* * * public interface BinaryOperator<T> extends BiFunction<T,T,T> { * * 返回2個比較參數之間的較小值 * public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) { * Objects.requireNonNull(comparator); * return (a, b) -> comparator.compare(a, b) <= 0 ? a : b; * } * * 返回2個比較參數之間的較大值 * public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) { * Objects.requireNonNull(comparator); * return (a, b) -> comparator.compare(a, b) >= 0 ? a : b; * } * } */ System.out.println("繼承BiFunction的Apply方法 實現減法"); System.out.println("10 - 2 = "+this.testBinaryOperator(10, 2)); System.out.println("------------------------------\n"); System.out.println("字符串較短的是:"+this.testMinBy("Kirito","Asuna",length)); System.out.println("字符串較長的是:"+this.testMaxBy("Kirito","Asuna",length)); System.out.println("------------------------------\n"); System.out.println("字符串首字母ASCII碼較小的是:"+this.testMinBy("Kirito","Asuna",asc)); System.out.println("字符串首字母ASCII碼較大的是:"+this.testMaxBy("Kirito","Asuna",asc)); } }