行为参数化
package com.huhu.test.helloword.java8;
/**
* @author :小虎
* @date :2018/6/29
*/
public class Apple {
private String color;
private int weight;
public Apple(int weight,String color) {
this.color = color;
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
package com.huhu.test.helloword.java8;
/**
* @author :小虎
* @date :2018/6/29
*/
public class AppleGreenColorPredicate implements ApplePredicate{
@Override
public boolean test(Apple apple){
return "green".equals(apple.getColor());
}
}
package com.huhu.test.helloword.java8;
/**
* @author :小虎
* @date :2018/6/29
*/
public class AppleHeavyWeightPredicate implements ApplePredicate{
@Override
public boolean test(Apple apple){
return apple.getWeight() > 150;
}
}
package com.huhu.test.helloword.java8;
/**
* @author :小虎
* @date :2018/6/29
*/
public interface ApplePredicate{
boolean test (Apple apple);
}
package com.huhu.test.helloword.java8;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author :小虎
* @date :2018/6/29
*/
public class Test {
public static void main(String[] args) {
List<Apple> inventory = Arrays.asList(new Apple(80,"green"),
new Apple(155, "green"),
new Apple(120, "red"));
List<Apple> heavyApples = filterApples(inventory, new AppleHeavyWeightPredicate());
List<Apple> greenApples = filterApples(inventory, new AppleGreenColorPredicate());
System.out.println(heavyApples.size());
System.out.println(greenApples.size());
}
public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate p){
List<Apple> result = new ArrayList<>();
for(Apple apple: inventory){
if(p.test(apple)){
result.add(apple);
}
}
return result;
}
}
改进尝试一(匿名类)
List<Apple> redApples = filterApples(inventory, new ApplePredicate() {
public boolean test(Apple apple){
return "red".equals(apple.getColor());
}
});
改进尝试二(Lambda 表达式)
List<Apple> result =filterApples(inventory, (Apple apple) -> "red".equals(apple.getColor()));
改进尝试三(将 List 类型抽象化)
public interface Predicate<T>{
boolean test(T t);
}
public static <T> List<T> filter(List<T> list, Predicate<T> p){
List<T> result = new ArrayList<>();
for(T e: list){
if(p.test(e)){
result.add(e);
}
}
return result;
}
List<Apple> redApples =filter(inventory, (Apple apple) -> "red".equals(apple.getColor()));
List<Integer> evenNumbers =filter(numbers, (Integer i) -> i % 2 == 0);
Comparator
inventory.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
Runnable
Thread t = new Thread(() -> System.out.println("Hello world"));
函数式接口
一言以蔽之, 函数式接口就是只定义一个抽象方法的接口。你已经知道了Java API中的一些
其他函数式接口,如我们在第2章中谈到的Comparator和Runnable
public interface Comparator<T> {
int compare(T o1, T o2);
}
public interface Runnable{
void run();
}
public interface ActionListener extends EventListener{
void actionPerformed(ActionEvent e);
}
public interface Callable<V>{
V call();
}
public interface PrivilegedAction<V>{
V run();
}
Lambda表达式允许你直接以内联的形式为函数式接口的抽象方法提供实现, 并把整个表达式作为函数式接口的实例(具体说来,是函数式接口一个具体实现的实例)。