Java8新特性

苹果的例子

/**
 * @author mapleins
 * @Date 2018-12-12 12:31
 * @Desc 苹果的pojo 颜色、重量
 **/
public class Apple {

    private String color;

    private Long weight;
}

      需求:现在我们要找到绿色的苹果

    /**
     * 找到绿色的苹果
     */
    public static List<Apple> findGreenApple(List<Apple> apples){
        List<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            if("green".equals(apple.getColor())){
                list.add(apple);
            }
        }
        return list;
    }

      运行结果

    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(new Apple("green",150l),
                new Apple("red",120l),new Apple("green",170l));
        List<Apple> greenApples = findGreenApple(list);
        System.out.println(greenApples);
    }

结果:[Apple{color='green', weight=150}, Apple{color='green', weight=170}]

      当需求发生改变,我们又要红色的苹果,此时有需要定义新方法。但是当我们又有新需求,我们需要黄色的苹果,这样在需求发生改变后,就要不停的定义方法,那么尝试定义一种通用的方法。

    /**
     * 通用的方法,我们传入苹果集合的同时,传入需要找到的苹果的颜色
     */
    public static List<Apple> findApple(List<Apple> apples,String color){
        List<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            if(color.equals(apple.getColor())){
                list.add(apple);
            }
        }
        return list;
    }

    运行结果

    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(new Apple("green",150l),new Apple("red",120l),new Apple("green",170l));
        List<Apple> apples = findApple(list, "red");
        System.out.println(apples);
    }
结果:[Apple{color='red', weight=120}]

    此时,如果需求又发生改变,需要找到red的并且weight>100的苹果,那么我们又需要定义方法。如何根据需求的不同,一个方法能满足所有的条件呢

    采用策略模式的方式:

//1.定义过滤接口
/**
 * @author mapleins
 * @Date 2018-12-12 13:09
 * @Desc 过滤接口
 **/
public interface AppFilter {

    boolean filter(Apple apple);
    
}

//2.定义方法
    /**
     * 找到apple的方法
     */
public static List<Apple> findApple(List<Apple> apples,AppFilter appFilter){
     List<Apple> list = new ArrayList<>();
     for (Apple apple : apples) {
        if(appFilter.filter(apple)){
            list.add(apple);
        }
    }
    return list;
}

//3.测试
    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(new Apple("green",150l),new Apple("red",120l),new Apple("green",140l));
        //找到红色,并且重量大于100的苹果
        List<Apple> apples = findApple(list, new AppFilter() {
            @Override
            public boolean filter(Apple apple) {
                return ("red".equals(apple.getColor()) && apple.getWeight() > 100l);
            }
        });
        System.out.println(apples);
    }

//4.结果
[Apple{color='red', weight=120}]

  但是采用策略模式和匿名内部类代码体积太大,所以我们可以尝试lambda表达式

@FunctionalInterface //告诉编译器这是个函数式接口
public interface AppFilter {

    boolean filter(Apple apple);

}

/**
 * @author mapleins
 * @Date 2018-12-12 13:10
 * @Desc 通过策略模式,采用lambda表达式
 **/
public class AppFilter3 {

    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(new Apple("green",150l),new Apple("red",120l),new Apple("green",140l));
        //找到红色,并且重量大于100的苹果
        List<Apple> apples = AppFilter2.findApple(list, apple -> {
            return "red".equals(apple.getColor()) && apple.getWeight() > 100;
        });
        System.out.println(apples);
    }

}

创建线程

/**
 * @author mapleins
 * @Date 2018-12-12 13:34
 * @Desc 创建线程的方式
 **/
public class SetupThread {

    public static void main(String[] args) {
        //1.使用匿名内部类
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }).start();

        //2.lambda表达式
        new Thread(()-> System.out.println(Thread.currentThread().getName())).start();
    }
}

Lambda表达式

原文地址:https://www.cnblogs.com/mapleins/p/10107894.html