java8新特性——四大内置核心函数式接口

  在前面几篇简单介绍了一些Lambda表达式得好处与语法,我们知道使用Lambda表达式是需要使用函数式接口得,那么,岂不是在我们开发过程中需要定义许多函数式接口,其实不然,java8其实已经为我们定义好了4类内置函数式接口,这4类接口其实已经可以解决我们开发过程中绝大部分的问题,只有一小部分比较特殊得情况需要我们自己去定义函数式接口,本文就简单来学习一下java8内置得4大核心函数式接口。

 

一、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     }

  上面就是一个函数型接口,输入一个类型得参数,输出一个类型得参数,当然两种类型可以一致。

扫描二维码关注公众号,回复: 130748 查看本文章

四、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内置得核心函数式接口,其中包括了大部分得方法类型,所以可以在使用得时候根据不同得使用场景去选择不同得接口使用。

猜你喜欢

转载自www.cnblogs.com/wuyx/p/9000312.html