java8学习(一)

行为参数化

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表达式允许你直接以内联的形式为函数式接口的抽象方法提供实现, 并把整个表达式作为函数式接口的实例(具体说来,是函数式接口一个具体实现的实例)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值