lambda-expression.PNG
在 JDK8 提供函数式接口, 这样 java 这门语言也成为同时支持命令式编程, 面向对象编程以及函数式编程的多范式的开发语言. 今天通过示例代码给大家分享一下我们如何在实际开发中应用 JDK8 提供函数式接口来重构我们的代码.
- interface ITransFormat{
- String format(String input);
- }
- class MTrans{
- private final String input;
- public MTrans(String input) {
- this.input = input;
- }
- public void trans(ITransFormat iTransFormat){
- System.out.println(iTransFormat.format(input) + "is transformed to weichat format");
- }
- }
- public class FPDemoA {
- public static void main(String[] args) {
- MTrans trans = new MTrans("jianshu");
- trans.trans( input -> "content of" + input );
- }
上面代码大家可能再熟悉不过, 也没有什么新意. 这个的 public void trans(ITransFormat iTransFormat) 接收一个接口作为参数. 这个接口稍微有些不同, 这里我给大家描述一下他不同之处, 就是只有一个要实现的方法, 这方法有一个输入 String input 和一个输出 String. 那么显然这是一个函数式接口.
所以我们无需定义 ITransFormat 接口定义参数的类型, 直接就是一个函数式 Function<String,String > 来定义参数的类型. 如下
- class MTrans{
- private final String input;
- public MTrans(String input) {
- this.input = input;
- }
- public void trans(Function<String,String> iTransFormat){
- System.out.println(iTransFormat.apply(input) + "is transformed to weichat format");
- }
- }
这样我们就可以少声明一个接口来定义参数类型, 好处不仅这些, 这种函数式接口类型还支持 andThen 来进行扩展.
- public static void main(String[] args) {
- MTrans trans = new MTrans("jianshu");
- Function<String,String> handler = input -> "content of" + input;
- trans.trans( handler.andThen( body -> "written by zidea" + body) );
- }
JDK8 给我们提供许多函数式接口的类型
方法引用
Consumer<T> 就是一种消费型函数式接口, 因为只有输入没有输出, 我们定义函数如果只有输入就可以表示为这种方法引用
- Consumer<String> consumer = s -> System.out.println(s);
- Consumer<String> consumer = System.out::println;
将函数赋值个一个 consumer 的引用, 让后可以通过 accept 进行调用.
- Consumer<String> consumer = System.out::println;
- consumer.accept("recieve input");
静态方法的方法引用
- class Animal{
- private String name = "aAnimal";
- /**
- * 静态方法
- * @param animal
- */
- public static void walk(Animal animal){
- System.out.println(animal + "is walking");
- }
- @Override
- public String toString() {
- return this.name;
- }
- }
上面是静态方法的方法引用, 通过:: 将类的静态方法赋值给函数式接口类型的引用
- Consumer<Animal> animalConsumer = Animal::walk;
- Animal animal = new Animal();
- animalConsumer.accept(animal);
非静态方法, 使用对象实例来方法引用
- public int eat(int num){
- System.out.println("can eat" + num + "kg");
- this.food -= num;
- return this.food;
- }
下面是非静态方法的方法引用, 值得注意的是引用输入类型和输出类型都是 int 类型我们可以简化为 UnaryOperator<Integer> 表示接口
- Consumer<Animal> animalConsumer = Animal::walk;
- Animal animal = new Animal();
- animalConsumer.accept(animal);
- UnaryOperator<Integer> function = animal::eat;
- System.out.println("" + function.apply(2) +"kg");
因为已经确定类型为 Int 所以可以直接使用 IntUnaryOperator 这样从而省略泛型的判断.
- IntUnaryOperator function = animal::eat;
- System.out.println("" + function.applyAsInt(2) +"kg");
非静态方法, 使用的对象实例的方法引用, 实际上在方法 eat 中有一个隐式参数就是 Animal , 实例化时候调用构造函数创建一个 Animal 的实例作为名为 this 传入非静态方法.
- Animal animal = new Animal();
- animal.eat(1);
- public int eat(Animal this,int num){
- System.out.println("can eat" + num + "kg");
- this.food -= num;
- return this.food;
- }
JDK 默认会把当前实例传入到非静态方法, 参数名为 this, 位置是第一个
使用类名来引用方法
- BiFunction<Animal,Integer,Integer> eatFunc = Animal::eat;
- System.out.println(eatFunc.apply(animal,2));
构造函数方法引用
- Supplier<Animal> supplier = Animal::new;
- System.out.println("create a new" +supplier.get());
带参数的构造函数的方法引用
- public Animal(String name){
- this.name = name;
- }
- Function<String,Animal> function1 = Animal::new;
- function1.apply("duck");
来源: http://www.jianshu.com/p/556327757cce