java.util.function函数式接口应用举例

Java函数式接口介绍

Java8中有很多现成的函数式接口,它们在java.util.function包下,查看jdk文档,下图是部分的函数式接口:
在这里插入图片描述
前边学习Lambda知道,函数式接口只有一个抽象方法,下边打开函数接口Consumer源代码,如下:

package java.util.function;

import java.util.Objects;

@FunctionalInterface
public interface Consumer<T> {
     void accept(T t);
     default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

Consumer接口只有一个void accept(T t);抽象方法。并且使用@FunctionalInterface注解标注它是一个函数式接口。(关于注解的知识点后边会介绍,这里只需要知道有@FunctionalInterface的接口都是函数式接口)

注意:如果某个接口只有一个抽象方法,但没有@FunctionalInterface标注,它也是函数接口。

这些函数接口是针对不同应用场景而设计,下边主要测试几种具有一定代表的函数接口,如下:
在这里插入图片描述

Function

1)Function源代码

查看Function的源代码,如下:

package java.util.function;

import java.util.Objects;

@FunctionalInterface
public interface Function<T, R> {
	R apply(T t);
	
	default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }
	default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }
     static <T> Function<T, T> identity() {
        return t -> t;
    }
}

Function拥有一个抽象方法,两个默认方法,一个静态方法,接收泛型参数,一个是apply方法的输入参数类型,一个是apply方法的输出结果类型。

Function主要用于转换作用,输入一个类型的参数,输出另一个类型的结果。

2)apply方法测试

package com.pbteach.javase.oop.lambda.test2;

import java.util.function.Function;

/**
 * 测试java.util.function.Function接口
 * @author 攀博课堂(www.pbteach.com)
 * @version 1.0
 **/
public class TestFunction {

    public static void main(String[] args) {
        testApply();

    }

    public static void testApply(){
        Function<String,Integer> function1 = (String p1)->{
            return p1.length();
        };
        //简写
        Function<String,Integer> function2 = p1->p1.length();

        System.out.println(function1.apply("www.pbteach.com"));
        System.out.println(function2.apply("www.pbteach.com"));
    }
}

输出:

15
15

3)andThen方法测试

Function接口还有两个默认方法:andThen、compose,下边测试andThen方法,它的源代码如下:

	default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

andThen方法传入一个Function接口实例,返回一个Function接口实例。它可以实现先执行当前Function接口实现的apply方法,再执行after实例的apply方法,实现前后执行两个操作的功能。

测试代码如下:

    //测试andThen方法
    public static void testAndThen(){
        //前边的操作
        Function<String,String> before = s1 -> "www."+s1;

        //后边的操作
        Function<String,String> after = s1 -> s1 + ".com";
        //调用andThen方法分别执行前、后两个操作
        String pbteach = before.andThen(after).apply("pbteach");
        System.out.println(pbteach);

    }

输出:

www.pbteach.com

画图分析:

在这里插入图片描述

BiFunction

下边测试其中一个接口BiFunction<T,U,R>,查看API文档如下:
本接口定义三个泛型参数:T、U、R,抽象方法有两个参数和返回值,查看源代码如下:

@FunctionalInterface
public interface BiFunction<T, U, R> {

    /**
     * Applies this function to the given arguments.
     *
     * @param t the first function argument
     * @param u the second function argument
     * @return the function result
     */
    R apply(T t, U u);
    ...

使用Lambda测试此接口,Lambda要有两个参数及返回值,代码如下:

package com.pbteach.javase.oop.lambda.test2;

import java.util.function.BiFunction;

/**
 * 测试BiFunction
 * @author 攀博课堂(www.pbteach.com)
 * @version 1.0
 **/
public class TestBiFunction {

    public static void main(String[] args) {
        //求和
        BiFunction<Integer,Integer,Integer> fun1 = (x,y)->x+y;
        //乘积
        BiFunction<Integer,Integer,Integer> fun2 = (x,y)->x*y;
        System.out.println(operate(fun1,1,2));
        System.out.println(operate(fun2,1,2));
    }

    //两个数运算
    public static int operate(BiFunction<Integer,Integer,Integer> fun,int n1,int n2){
        return fun.apply(n1,n2);
    }
}

输出:

3
2

Consumer

Consumer介绍

查看Consumer接口的源代码,如下:

package java.util.function;

import java.util.Objects;
@FunctionalInterface
public interface Consumer<T> {

	void accept(T t);
	
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

Consumer的抽象方法void accept(T t)只接收一个参数,没有返回值。Consumer接口是一个消费接口,接收一个数据进行消费。

Consumer测试

有一个整数数列,下边使用Consumer接口进行消费,测试代码如下:

package com.pbteach.javase.oop.lambda.test2;

import java.util.function.Consumer;

/**
 * 测试Consumer接口
 * @author 攀博课堂(www.pbteach.com)
 * @version 1.0
 **/
public class TestConsumer {

    static int[] datas = {1,2,3,4,5,6,7};
    static int sum = 0;
    public static void main(String[] args) {
        //消费方式是输出数据
        Consumer<Integer> fun1 = n -> System.out.println(n);
        //判断奇偶
        Consumer<Integer> fun2 = n -> {
            if(n % 2 ==0){
                System.out.println(n + "是偶数");
            }else{
                System.out.println(n + "是奇数");
            }
        };
        //求累加和
        Consumer<Integer> fun3 = n -> sum+=n;

        consumer(fun1,datas);
        consumer(fun2,datas);
        consumer(fun3,datas);
        System.out.println("sum="+sum);

    }

    public static void consumer(Consumer<Integer> fun,int[] datas){
        for (int i = 0; i < datas.length; i++) {
            fun.accept(datas[i]);
        }
    }
}

输出:

1
2
3
4
5
6
7
1是奇数
2是偶数
3是奇数
4是偶数
5是奇数
6是偶数
7是奇数
sum=28

更多函数式接口应用

请参考Lambda函数式接口教程(全免费):http://www.pbteach.com/course/20/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值