四大内置核心函数式接口

* JAVA8 内置的四大核心函数式接口
*
* Consumer<T> :消费型接口
*          void accept(T t);
* Supplier<T> :供给型接口
*          T get();
* Function<T, R> :函数型接口
*          R apply(T t);
* Predicate<T>   :断言型接口
*          boolean test(T t);

一、

Consumer<T> 消费型接口
    //Consumer<T> 消费型接口
    @Test
    public void test1(){
        happy(1000,(m) -> System.out.println("今天出去玩,花了"+m+"元"));
    }
    public void happy(double money, Consumer con){
        con.accept(money);
    }

运行结果:

今天出去玩,花了1000.0元

二、

Supplier<T> :供给型接口
 //Supplier<T> :供给型接口
    @Test
    public void test2(){
        List<Integer> list = getNumList(10, () -> (int) (Math.random() * 100));
        for (Integer num : list) {
            System.out.println(num);
        }
    }
    //需求:产生一些整数,并放入集合中
    public List<Integer> getNumList(int num,Supplier<Integer> sup){
        List<Integer> list = new ArrayList<>();
        for (int i =0; i<num; i++){
            Integer n =  sup.get();
            list.add(n);
        }
        return list;
    }

运行结果:

64
78
40
16
0
18
82
73
36
80

三、

Function<T, R> :函数型接口
//Function<T, R> :函数型接口
    @Test
    public void test3() {
        //去除前后空格
        String trimStr = strHandler("\t\t\t   学习java8新特性  ", (str) -> str.trim());
        System.out.println(trimStr);
        String subStr = strHandler("学习java8新特性", (str) -> str.substring(2,7));//包头不包尾[2,7)
        System.out.println(subStr);
    }
    //需求:用于处理字符串
    public String strHandler(String str,Function<String,String> fun) {
         return fun.apply(str);
    }

运行结果:

学习java8新特性
java8

四、

Predicate<T>   :断言型接口
//Predicate<T>   :断言型接口
    @Test
    public void test4(){
        List<String> list = Arrays.asList("Consumer","Supplier","Function","Predicate","haha","hai");
        List<String> filterStr = filterStr(list, (s) -> s.length() > 3);
        for (String str: filterStr){
            System.out.println(str);
        }
    }
    //需求: 将满足条件的字符串添加到集合中
    public List<String> filterStr(List<String> list,Predicate<String> pre){
        List<String> strList = new ArrayList<>();
        for (String str: list) {
            if (pre.test(str)){
                strList.add(str);
            }
        }
        return strList;
    }

运行结果:

Consumer
Supplier
Function
Predicate
haha

JAVA8 内置的四大核心函数式接口能够满足大多数情景下的需求,当然如果觉得这几个还是不够用,那么可以参考下面的一些拓展的接口,其用法与上面大同小异:

序号 接口 & 描述
1 BiConsumer<T,U>

代表了一个接受两个输入参数的操作,并且不返回任何结果

2 BiFunction<T,U,R>

代表了一个接受两个输入参数的方法,并且返回一个结果

3 BinaryOperator<T>

代表了一个作用于于两个同类型操作符的操作,并且返回了操作符同类型的结果

4 BiPredicate<T,U>

代表了一个两个参数的boolean值方法

5 BooleanSupplier

代表了boolean值结果的提供方

6 Consumer<T>

代表了接受一个输入参数并且无返回的操作

7 DoubleBinaryOperator

代表了作用于两个double值操作符的操作,并且返回了一个double值的结果。

8 DoubleConsumer

代表一个接受double值参数的操作,并且不返回结果。

9 DoubleFunction<R>

代表接受一个double值参数的方法,并且返回结果

10 DoublePredicate

代表一个拥有double值参数的boolean值方法

11 DoubleSupplier

代表一个double值结构的提供方

12 DoubleToIntFunction

接受一个double类型输入,返回一个int类型结果。

13 DoubleToLongFunction

接受一个double类型输入,返回一个long类型结果

14 DoubleUnaryOperator

接受一个参数同为类型double,返回值类型也为double 。

15 Function<T,R>

接受一个输入参数,返回一个结果。

16 IntBinaryOperator

接受两个参数同为类型int,返回值类型也为int 。

17 IntConsumer

接受一个int类型的输入参数,无返回值 。

18 IntFunction<R>

接受一个int类型输入参数,返回一个结果 。

19 IntPredicate

:接受一个int输入参数,返回一个布尔值的结果。

20 IntSupplier

无参数,返回一个int类型结果。

21 IntToDoubleFunction

接受一个int类型输入,返回一个double类型结果 。

22 IntToLongFunction

接受一个int类型输入,返回一个long类型结果。

23 IntUnaryOperator

接受一个参数同为类型int,返回值类型也为int 。

24 LongBinaryOperator

接受两个参数同为类型long,返回值类型也为long。

25 LongConsumer

接受一个long类型的输入参数,无返回值。

26 LongFunction<R>

接受一个long类型输入参数,返回一个结果。

27 LongPredicate

R接受一个long输入参数,返回一个布尔值类型结果。

28 LongSupplier

无参数,返回一个结果long类型的值。

29 LongToDoubleFunction

接受一个long类型输入,返回一个double类型结果。

30 LongToIntFunction

接受一个long类型输入,返回一个int类型结果。

31 LongUnaryOperator

接受一个参数同为类型long,返回值类型也为long。

32 ObjDoubleConsumer<T>

接受一个object类型和一个double类型的输入参数,无返回值。

33 ObjIntConsumer<T>

接受一个object类型和一个int类型的输入参数,无返回值。

34 ObjLongConsumer<T>

接受一个object类型和一个long类型的输入参数,无返回值。

35 Predicate<T>

接受一个输入参数,返回一个布尔值结果。

36 Supplier<T>

无参数,返回一个结果。

37 ToDoubleBiFunction<T,U>

接受两个输入参数,返回一个double类型结果

38 ToDoubleFunction<T>

接受一个输入参数,返回一个double类型结果

39 ToIntBiFunction<T,U>

接受两个输入参数,返回一个int类型结果。

40 ToIntFunction<T>

接受一个输入参数,返回一个int类型结果。

41 ToLongBiFunction<T,U>

接受两个输入参数,返回一个long类型结果。

42 ToLongFunction<T>

接受一个输入参数,返回一个long类型结果。

43 UnaryOperator<T>

接受一个参数为类型T,返回值类型也为T。

猜你喜欢

转载自blog.csdn.net/m0_37450089/article/details/81416680