1. 文件筛选
Java8之前的写法
1 File[] hiddenfiles = new file(".").listFiles(newFileFilter(){2 public booleanaccept(File file){3 returnfile.isHidden();4 }5 });
需要通过在一个匿名类中定义筛选的方法,并实例化这个类,将其作为参数传递到方法中。
Java8中的写法
File[] hiddenFiles = new File(".").listFiles(File::isHidden);
在Java8中写下File::isHidden的时候,相当于创建了一个方法引用,可以将他传递给方法。将方法作为值构成了Java8中许多功能的基础。
2. 传递代码:一个例子
当两块代码中只有一部分存在差异
方法一
1 public static List filterGreenApples(Listinventory){2 List result = new ArrayList<>();3 for(Apple apple: inventory){4 if ("green".equals(apple.getColor())) {5 result.add(apple);6 }7 }8 returnresult;9 }
方法二
1 public static List filterHeavyApples(Listinventory){2 List result = new ArrayList<>();3 for(Apple apple: inventory){4 if (apple.getWeight() > 150) {5 result.add(apple);6 }7 }8 returnresult;9 }
方法一和方法二只有第4行的判断规则不一样,可以将其抽象,将差异抽象出来之后定义成
方法三
1 static List filterApples(Listinventory,Predicatep) {2 List result = new ArrayList<>();3 for(Apple apple: inventory){4 if(p.test(apple)) {5 result.add(apple);6 }7 }
8 returnresult;9 }
Predicate是java.util.function包中定义的函数式接口,即只定义一个抽象方法的接口,接口可以包含默认方法。
Lambda表达式允许直接以内联的形式为函数式接口的抽象方法提供实现,并把整个表达式作为函数式接口的实现。
@FunctionalInterface用于表示改接口会被设计成一个函数式接口,编译器会对接口做检查。
1 @FunctionalInterface2 public interface Predicate{3 booleantest(T t);4 }
在某个类中定义方法一和方法二中不同的筛选规则
classAppleSupport{public static booleanisGreenApple(Apple apple) {return "green".equals(apple.getColor());
}public static booleanisHeavyApple(Apple apple) {return apple.getWeight() > 150;
}
}
调用时
1 filterApples(inventory, AppleSupport::isGreenApple);2 filterApples(inventory, AppleSupport::isHeavyApple);
使用Lambda代替方法定义
filterApples(inventory,(Apple a)->"green".equals(a.getColor()));
filterApples(inventory,(Apple a)->a.getWeight>150);
行为参数化就是一个方法接收多个不同的行为作为参数,在内部使用他们,完成不同行为的能力,类似于策略模式。