逐步走向响应式编程(三)-常见函数式接口- Function<T, R>

前面介绍了函数式接口PredicateConsumer,本文继续介绍另一函数式接口Function<T, R>,此接口同样也位于 java.util.function包中,它的作用可以看作是消费者和生产者的结合,即消费某种原材料然后生产出某种产品,继续先分析一下源码。

源码解析

package java.util.function;

import java.util.Objects;

/**
 * Represents a function that accepts one argument and produces a result.
 * 此接口中的方法接收一个参数并输出一个结果
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #apply(Object)}.
 *
 * @param <T> the type of the input to the function
 * @param <R> the type of the result of the function
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     * 此方法对输入的参数进行处理,并得到一个结果
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);

    /**
     * Returns a composed function that first applies the {@code before}
     * function to its input, and then applies this function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     * 此方法将两个function进行组合,得到一个新的function,组合的过程中如果有一个function出现了异常,异常由调用者来处理。组合逻辑是,将其中一个function以参数的形式传入(before),在compose内部获取before的结果,并传给第二个function组成一个新的function并返回
     * @param <V> the type of input to the {@code before} function, and to the
     *           composed function
     * @param before the function to apply before this function is applied
     * 这句话好难理解,简单来就说就是将一个function当作一个参数
     * @return a composed function that first applies the {@code before}
     * function and then applies this function
     * 将before function的结果传给当前function,并返回。
     * @throws NullPointerException if before is null
     *
     * @see #andThen(Function)
     */
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    /**
     * Returns a composed function that first applies this function to
     * its input, and then applies the {@code after} function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *此函数和compose函数有点类似,但顺序不同。此函数是先执行调用者,再执行andThen的参数,即将调用者的结果传给参数中的function使用,并返回一个function.
     * @param <V> the type of output of the {@code after} function, and of the
     *           composed function
     * @param after the function to apply after this function is applied
     * @return a composed function that first applies this function and then
     * applies the {@code after} function
     * @throws NullPointerException if after is null
     *
     * @see #compose(Function)
     */
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    /**
     * Returns a function that always returns its input argument.
     *将apply的参数当着结果返回出来
     * @param <T> the type of the input and output objects to the function
     * @return a function that always returns its input argument
     */
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

案例分析

通过处理User案例来分析每个方法的使用。提供了两个类User 和 UserEntity:
User

public class User {
    private int age;
    private String name;
    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }
    /**
     * @param age
     * @param name
     */
    public User(int age, String name) {
        this.age = age;
        this.name = name;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    
}

UserEntity

public class UserEntity {
    private int age;
    private String name;
    private int type;
    /**
     * @param age
     * @param name
     * @param type
     */
    public UserEntity(int age, String name, int type) {
        this.age = age;
        this.name = name;
        this.type = type;
    }
    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the type
     */
    public int getType() {
        return type;
    }
    /**
     * @param type the type to set
     */
    public void setType(int type) {
        this.type = type;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    
    @Override
    public String toString() {
        return "UserEntity {age=" + age + ", name=" + name + ", type=" + type + "}";
    }
    
}

apply

将一组User转换成一组User Entity, 根据User的年龄来定义用户级别(普通用户,vip,svip),其中用户级别是User Entity的一个字段,所以输入参数是List,返回结果是List 。

构造数据
        User user1 = new User(10, "张三");
        User user2 = new User(15, "李四");
        User user3 = new User(16, "王五");
        User user4 = new User(20, "赵六");
        User user5 = new User(25, "田七");
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        users.add(user5);
定义转换逻辑
 /**
     * Convert users to user entities 
     * age<=15:GENERAL
     * 15<age<=20:VIP
     * 20<age:SVIP
     * @param Function<List<User>,List<UserEntity>>
     */
    static Function<List<User>,List<UserEntity>> multiUsersToEntities(List<User> users){
        Function<List<User>,List<UserEntity>> function = t-> {
            List<UserEntity> userEntities = new ArrayList<>();
            int age;
            String name;
            UserEntity userEntity;
            for(User user:t){
                age = user.getAge();
                name = user.getName(); 
                if( age<=15){
                     userEntity = new UserEntity(age, name, Type.GENERAL.getCode());   
                }else if (age > 15 && age <=20){
                    userEntity = new UserEntity(age, name, Type.VIP.getCode());   
                }else{
                    userEntity = new UserEntity(age, name, Type.SVIP.getCode());   
                }
                userEntities.add(userEntity);
            }
            return userEntities;
        };
        return function;
    }
开始转换
        Function<List<User>,List<UserEntity>> function = multiUsersToEntities(users);
        List<UserEntity> uEntities = function.apply(users);
转换结果
uEntities.stream().forEach(u->System.out.println(u.toString()));

转换成功后的结果如下

UserEntity {age=10, name=张三, type=3}
UserEntity {age=15, name=李四, type=3}
UserEntity {age=16, name=王五, type=1}
UserEntity {age=20, name=赵六, type=1}
UserEntity {age=25, name=田七, type=2}

compose

根据注释中的理解感觉一脸蒙逼,这里简单来说就是将两个函数进行组合,先执行compose里面的函数再执行外面的函数,为了便于理解脱离上面的例子。
定义两个函数如下:

//对输入的值进行两倍处理
Function<Integer,Integer> function2 = t-> t*2;
//对输入的值进行平方处理
Function<Integer,Integer> function3 = t-> t*t;
System.out.println("compose result :"+function2.compose(function3).apply(3));

结果为:compose result :18

andThen

同compose类似,也是将两个函数组合起来使用,但是顺序不一样,这里先执行andThen外面的函数,再执行andThen里面的函数。继续以上面的两个函数为例

System.out.println("andThen result :"+function2.andThen(function3).apply(3));

结果为:andThen result :36

identity

identiy个人没用过,这里举个例子吧,以后深入了解之后继续更新。

Function<Integer,Integer> function4 = Function.identity()System.out.println("identity result :"+function4.apply(3));

结果为:identity result :3

总结

内容很简单,在java8的Stream中,Function使用很广泛,后续的博文中会一一介绍。最后希望本文能帮助大家,祝大家在IT之路上少走弯路,一路绿灯不堵车,测试一性通过,bug秒解!
源码下载

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浦江之猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值