JDK8新特性1——Lambda表达式引入

        首先看一个例子,有一个Apple类,有两个属性,color与weight,我们需要在一个List集合中找到找到color=green的所有的Apple。

package com.bjc.demo1;

public class Apple {
    private String color;
    private Long weight;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Long getWeight() {
        return weight;
    }

    public void setWeight(Long weight) {
        this.weight = weight;
    }

    public Apple(String color, Long weight) {
        this.color = color;
        this.weight = weight;
    }

    public Apple() {
    }

    @Override
    public String toString() {
        return "Apple{" +
                "color='" + color + '\'' +
                ", weight=" + weight +
                '}';
    }
}

1 直接定义方法实现

package com.bjc.demo1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
    public static List<Apple> findGreenApple(List<Apple> apples){
        List<Apple> list = new ArrayList<>();
        for(Apple a : apples){
            if(a.getColor().compareToIgnoreCase("Green") == 0){
                list.add(a);
            }
        }
        return list;
    }


    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(
                new Apple("yellow",100L),
                new Apple("green",100L),
                new Apple("red",100L),
                new Apple("green",100L),
                new Apple("pink",100L),
                new Apple("gray",100L),
                new Apple("green",100L)
        );
        List<Apple> apples = findGreenApple(list);
        System.out.println(apples);
    }
}

2 抽取接口来实现

接口AppleFilter.java定义

package com.bjc.demo1;

public interface AppleFilter {
    Boolean colorFilter(Apple apple);
}

接口AppleFilter实现

package com.bjc.demo1;

public class MyAppleFilter implements AppleFilter{
    @Override
    public Boolean colorFilter(Apple apple) {
        return "green".compareToIgnoreCase(apple.getColor()) == 0;
    }

}

业务实现代码:

package com.bjc.demo1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {

    public static List<Apple> findApple(List<Apple> apples,AppleFilter filter){
        List<Apple> list = new ArrayList<>();
        for(Apple a : apples){
            if(filter.colorFilter(a)){
                list.add(a);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(
                new Apple("yellow",100L),
                new Apple("green",100L),
                new Apple("red",100L),
                new Apple("green",100L),
                new Apple("pink",100L),
                new Apple("gray",100L),
                new Apple("green",100L)
        );
        List<Apple> apples = findApple(list,new MyAppleFilter());
        System.out.println(apples);

    }
}

3 匿名内部类方式实现

接口定义:

package com.bjc.demo1;

public interface AppleFilter {
    Boolean colorFilter(Apple apple);
}

业务实现

package com.bjc.demo1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {

    public static List<Apple> findApple(List<Apple> apples,AppleFilter filter){
        List<Apple> list = new ArrayList<>();
        for(Apple a : apples){
            if(filter.colorFilter(a)){
                list.add(a);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(
                new Apple("yellow",100L),
                new Apple("green",100L),
                new Apple("red",100L),
                new Apple("green",100L),
                new Apple("pink",100L),
                new Apple("gray",100L),
                new Apple("green",100L)
        );
        List<Apple> apples = findApple(list, new AppleFilter() {
            @Override
            public Boolean colorFilter(Apple apple) {
                return "green".compareToIgnoreCase(apple.getColor()) == 0;
            }
        });
        System.out.println(apples);
    }
}

4 Lambda表达式实现

接口定义:

package com.bjc.demo1;

public interface LamFilter {
    Boolean filter(Apple apple);
}

业务实现:

package com.bjc.demo1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
    public static List<Apple> findApple(List<Apple> apples,LamFilter lamFilter){
        List<Apple> list = new ArrayList<>();
        for(Apple a : apples){
            if(lamFilter.filter(a)){
                list.add(a);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(
                new Apple("yellow",100L),
                new Apple("green",100L),
                new Apple("red",100L),
                new Apple("green",100L),
                new Apple("pink",100L),
                new Apple("gray",100L),
                new Apple("green",100L)
        );
        List<Apple> appleList = findApple(list, (Apple apple) -> {
            return apple.getColor().equals("green");
        });
        System.out.println(appleList);
    }
}

注意:

1. 这里的Lambda表达式有参数推导功能,所以这里我们不必写(Apple apple),只需要写(apple)即可,又,参数只有一个的时候,括号可以省略,所以,这里可以写成 apple ->{ ... }的形式

2. 使用Lambda表达式,接口满足的条件是有且只有一个方法,default方法除外。

3. 为了严谨,在接口上加上注解@FunctionalInterface,可以防止在接口中有额外的方法。

例如:

package com.bjc.demo1;

@FunctionalInterface
public interface LamFilter {
    Boolean filter(Apple apple);
}

说到这里,之前我们创建多线程的时候的方式也可以使用Lambda来实现,例如:

new Thread( () -> {
    System.out.println(Thread.currentThread().getName());
}).start();
发布了205 篇原创文章 · 获赞 9 · 访问量 7953

猜你喜欢

转载自blog.csdn.net/weixin_43318134/article/details/104163668