<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] # 內建函數式接口 對于方法引用,嚴格來講需要定義一個接口,可是不管如何操作,實際上有可能操作的接口只有4種 jdk里面提供了一個包: **java.util.function**,提供有以下四個核心接口 ~~~ 1. 功能性接口(Function): public interface Function<T, R> {public R apply(T t);} | |---此接口需要接收一個參數,并且返回一個處理結果 2. 消費型接口(Consumer): public interface Consumer<T>{public void accept(T t);} | |---此接口只是負責接收數據(引用數據是不需要返回),并且不返回處理結果 |-- 如果方法的參數和返回值全都是Consumer類型,可以做一個操作后再做一個操作,實現組合,就是consumer的默認方法andThen 3. 供給型接口(Supplier): public interface Supplier<T> {public T get();} | |---此接口不接收參數,但是可以返回結果 4. 斷言型接口(Predicate): public interface Predicate<T> {public boolean test(T t);} | |---此接口進行判斷操作使用 ~~~ 所有在JDK1.8之中由于存在有以上的四個功能型接口,所以一般很少由用戶去定義函數式接口 ## 功能性接口Function 用來根據一個類型的數據得到另一個類型的數據,前者成為前置條件,后者成為后置條件 String類有一個方法: `public boolean startsWith(String str)` ~~~ //用內建函數式進行引用 Function<String, Boolean> fun = "#hello"::startsWith; //判斷是不是 # 開頭 System.out.println(fun.apply("#")); //true ~~~ ~~~ public static void main(String[] args) { method(s -> Integer.parseInt(s)); } public static void method(Function<String, Integer> function) { int num = function.apply("10"); System.out.println(num + 20); } ~~~ **默認接口: andThen** Function接口中有一個默認的andThen方法,用來進行組合操作. 用于先做什么再做什么和consumer中andThen差不多 ~~~ default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } ~~~ ~~~ public static void main(String[] args) { //字符串轉為int,再乘以10 method(Integer::parseInt, i -> i *= 10); } public static void method(Function<String, Integer> one, Function<Integer, Integer> two) { int num = one.andThen(two).apply("10"); System.out.println(num + 20); } ~~~ ## 消費型接口Consumer 消費一個數據,消費數據的類型由泛型指定 ~~~ package com.study; import java.util.function.Consumer; public class HelloWorld { public static void main(String[] args) { Consumer<String> cons = System.out::println; cons.accept("hello"); } } ~~~ **默認方法andThen** 如果方法的參數和返回值全都是Consumer類型,可以做一個操作后再做一個操作,實現組合,就是consumer的默認方法andThen jdk中源碼 ~~~ default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } ~~~ andThen的語義是一步接一步操作 **注意: java.util.objects的requireNonNull靜態方法將會在參數為null時候主動拋出NullPointerException異常,這省去了重復編寫if語句和拋出空指針異常的麻煩.** ~~~ public class App { public static void main(String[] args) { String[] arr = {"迪麗熱巴,女", "古力娜扎,女", "馬爾扎哈,男"}; printInfo(arr, (message) -> { String name = message.split(",")[0]; System.out.println("姓名: " + name); }, (message) -> { String age = message.split(",")[1]; System.out.println("年齡: " + age); }); } public static void printInfo(String[] arr, Consumer<String> con1, Consumer<String> con2) { //遍歷字符串 for (String message : arr) { //使用andThen方法連接兩個Consumer接口,消費字符串 con1.andThen(con2).accept(message); } } } ~~~ ## 供給型接口Suppiler 對外提供一個符合泛型類型對象數據. 使用String類的toUpperCase()方法: `public String toUpperCase();` ~~~ package com.study; import java.util.function.Supplier; public class HelloWorld { public static void main(String[] args) { Supplier<String> supplier = "helllo" :: toUpperCase; System.out.println(supplier.get()); //HELLLO } } ~~~ ## 斷言型接口Predicate 有時候我們需要對某種類型的數據進行判斷,從而得到一個boolean值結果.就可以用這個. String類里面有一個equalsIgnoreCase()方法 ~~~ package com.study; import java.util.function.Predicate; public class HelloWorld { public static void main(String[] args) { Predicate<String> pre = "hello"::equalsIgnoreCase; System.out.println(pre.test("Hello")); } } ~~~ ~~~ public static void main(String[] args) { String s = "abcdef"; boolean b = checkString(s, (str) -> str.length() > 5); System.out.println(b); } public static boolean checkString(String s, Predicate<String> pre) { return pre.test(s); } ~~~ **默認方法: and** 既然是條件判斷,就會存在與,或,非三種常見的邏輯關系.其中將兩個Predicate條件使用"與"邏輯連接起來實現"并且"的效果時,可以使用and ~~~ default Predicate<T> and(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) && other.test(t); ~~~ ~~~ public static void main(String[] args) { method(s -> s.contains("h"), s -> s.contains("w")); } private static void method(Predicate<String> one, Predicate<String> two) { boolean bool = one.and(two).test("hello world"); System.out.println("字符串符合要求嗎: " + bool); } ~~~ **默認方法: or** 與and的"與"類似,默認方法or實現邏輯關系中的"或".jdk源碼為: ~~~ default Predicate<T> or(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> test(t) || other.test(t); } ~~~ 如果希望實現邏輯字符串包含h或者包含w,那么代碼只要改為or就可以 ~~~ public static void main(String[] args) { method(s -> s.contains("h"), s -> s.contains("w")); } private static void method(Predicate<String> one, Predicate<String> two) { boolean bool = one.or(two).test("hello world"); System.out.println("字符串符合要求嗎: " + bool); } ~~~ **默認方法: negate** 取反 ~~~ default Predicate<T> negate() { return (t) -> !test(t); } ~~~ ~~~ public static void main(String[] args) { String s = "abc"; boolean b = checkString(s, (String str) -> { //判斷是否大于5 return str.length() > 5; }); System.out.println(b); } public static boolean checkString(String s, Predicate<String> pre) { //取反 return !pre.test(s); } ~~~ ~~~ public static void main(String[] args) { Predicate predicate = (i) -> i > 5 ? true : false; method(predicate.negate()); } public static void method(Predicate one) { boolean test = one.test(10); System.out.println(test); } ~~~ 除了這幾個還有其他和他類似的接口在jdk包下 # 其他接口 ![](https://img.kancloud.cn/66/68/66684f5cc03f11799494c580fdd147a8_1602x936.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看