【Java 8 新特性】Java 8中的Function.apply方法

Java 8中的Function.apply方法


java.util.function.Function是一个接口,已经在 Java 8中引入。

Function是一个函数接口。

因此它可以用来接受lambda表达式。

函数接受一个参数并返回结果。

函数接口包含一种方法apply()

这是函数接口方法。

查找apply()方法的声明。

R apply(T t)

其中T是函数参数,R是结果。

要使用它,我们需要定义功能。

假设我们在学生类中有一个方法customShow(),它将接受Function实例。

查找学生类。

Student.java

import java.util.function.Function;
public class Student {
    public String name;
    public int age;
    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }
    public  String customShow(Function<Student,String> fun){
        return fun.apply(this);
    }
}

有一个接受函数接口的customShow()方法。

现在,我们可以通过三种方式创建函数实例。

for(Student st: list){
    System.out.println(st.customShow(s->s.name+": "+s.name));
}

这是第一种方式。只需将s->s.name+": "+s.name作为Function实例传递给customShow方法。

Function<Student,String> styleOne = s->{
     String result =  "Name:"+s.name +" and Age:"+s.age;
     return result;
};

在第二种方法中,我们在单独的位置声明Function并操纵结果,最后返回。

Function<Student,String> styleTwo = s->        
            "Name:"+s.name +" and Age:"+s.age;

第三种方式,在一行中定义函数。

因此,customShow()方法将接受Function实例,而Functionapply()方法将执行如何定义函数。

查找完整的示例。

FunctionDemo.java

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class FunctionDemo {
    public static void main(String[] args) {
        List<Student> list = new ArrayList();
        list.add(new Student("Ram",20));
        list.add(new Student("Shyam",22));
        list.add(new Student("Kabir",18));
        
        // Simple use of function
        for(Student st: list){
            System.out.println(st.customShow(s->s.name+": "+s.name));
        }
        
        //Style one to declare function 
        Function<Student,String> styleOne = s->{
            String result =  "Name:"+s.name +" and Age:"+s.age;
            return result;
        };
        
        //Style two to declare function
        Function<Student,String> styleTwo = s->        
            "Name:"+s.name +" and Age:"+s.age;
        
        System.out.println("--print value by style one--");
        //print the values of list using stle one function
        for(Student st: list){
            System.out.println(st.customShow(styleOne));
        }
        
        System.out.println("--print value by style two--");
        //print the values of list using style two function
        for(Student st: list){
            System.out.println(st.customShow(styleTwo));
        }
        
    }
} 

输出

Ram: Ram
Shyam: Shyam
Kabir: Kabir
--print value by style one--
Name:Ram and Age:20
Name:Shyam and Age:22
Name:Kabir and Age:18
--print value by style two--
Name:Ram and Age:20
Name:Shyam and Age:22
Name:Kabir and Age:18

参考文献

【1】Function.apply in Java 8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

猫巳

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

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

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

打赏作者

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

抵扣说明:

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

余额充值