java8 四大內置核心函數式接口


 轉載自:https://www.cnblogs.com/wuyx/p/9000312.html

 其他補充接口:

 

一、Consumer<T>:消費型接口(void accept(T t))

  來看一個簡單得例子:

復制代碼
 1     /**
 2      * 消費型接口Consumer<T>
 3      */
 4     @Test
 5     public void test1 () {
 6         consumo(500, (x) -> System.out.println(x));
 7     }
 8     
 9     public void consumo (double money, Consumer<Double> c) {
10         c.accept(money);
11     }
復制代碼

  以上為消費型接口,有參數,無返回值類型的接口。

二、Supplier<T>:供給型接口(T get())

  來看一個簡單得例子:

復制代碼
 1     /**
 2      * 供給型接口,Supplier<T>
 3      */
 4     @Test
 5     public void test2 () {
 6         Random ran = new Random();
 7         List<Integer> list = supplier(10, () -> ran.nextInt(10));
 8         
 9         for (Integer i : list) {
10             System.out.println(i);
11         }
12     }
13     
14     /**
15      * 隨機產生sum個數量得集合
16      * @param sum 集合內元素個數
17      * @param sup
18      * @return
19      */
20     public List<Integer> supplier(int sum, Supplier<Integer> sup){
21         List<Integer> list = new ArrayList<Integer>();
22         for (int i = 0; i < sum; i++) {
23             list.add(sup.get());
24         }
25         return list;
26     }
復制代碼

  上面就是一個供給類型得接口,只有產出,沒人輸入,就是只有返回值,沒有入參

三、Function<T, R>:函數型接口(R apply(T t))

  下面看一個簡單的例子:

復制代碼
 1     /**
 2      * 函數型接口:Function<R, T>
 3      */
 4     @Test
 5     public void test3 () {
 6         String s = strOperar(" asdf ", x -> x.substring(0, 2));
 7         System.out.println(s);
 8         String s1 = strOperar(" asdf ", x -> x.trim());
 9         System.out.println(s1);
10     }
11     
12     /**
13      * 字符串操作
14      * @param str 需要處理得字符串
15      * @param fun Function接口
16      * @return 處理之后得字符傳
17      */
18     public String strOperar(String str, Function<String, String> fun) {
19         return fun.apply(str);
20     }
復制代碼

  上面就是一個函數型接口,輸入一個類型得參數,輸出一個類型得參數,當然兩種類型可以一致。

四、Predicate<T>:斷言型接口(boolean test(T t))

  下面看一個簡單得例子:

復制代碼
 1     /**
 2      * 斷言型接口:Predicate<T>
 3      */
 4     @Test
 5     public void test4 () {
 6         List<Integer> l = new ArrayList<>();
 7         l.add(102);
 8         l.add(172);
 9         l.add(13);
10         l.add(82);
11         l.add(109);
12         List<Integer> list = filterInt(l, x -> (x > 100));
13         for (Integer integer : list) {
14             System.out.println(integer);
15         }
16     }
17     
18     /**
19      * 過濾集合
20      * @param list
21      * @param pre
22      * @return
23      */
24     public List<Integer> filterInt(List<Integer> list, Predicate<Integer> pre){
25         List<Integer> l = new ArrayList<>();
26         for (Integer integer : list) {
27             if (pre.test(integer))
28                 l.add(integer);
29         }
30         return l;
31     }
復制代碼

  上面就是一個斷言型接口,輸入一個參數,輸出一個boolean類型得返回值。

五、其他類型的一些函數式接口

  除了上述得4種類型得接口外還有其他的一些接口供我們使用:

    1).BiFunction<T, U, R> 

      參數類型有2個,為T,U,返回值為R,其中方法為R apply(T t, U u)

    2).UnaryOperator<T>(Function子接口)

      參數為T,對參數為T的對象進行一元操作,並返回T類型結果,其中方法為T apply(T t)

    3).BinaryOperator<T>(BiFunction子接口)

      參數為T,對參數為T得對象進行二元操作,並返回T類型得結果,其中方法為T apply(T t1, T t2)

    4).BiConsumcr(T, U) 

      參數為T,U無返回值,其中方法為 void accept(T t, U u)

    5).ToIntFunction<T>、ToLongFunction<T>、ToDoubleFunction<T>

      參數類型為T,返回值分別為int,long,double,分別計算int,long,double得函數。

    6).IntFunction<R>、LongFunction<R>、DoubleFunction<R>

      參數分別為int,long,double,返回值為R。

 

  以上就是java8內置得核心函數式接口,其中包括了大部分得方法類型,所以可以在使用得時候根據不同得使用場景去選擇不同得接口使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM