1. 程式人生 > 其它 >java8 lambda表示式

java8 lambda表示式

java8 設定

實體類

public class Apple {

    private String color;
    private double weight;

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

    public double getWeight() {
        return weight;
    }

    public void setHeigh(double weight) {
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

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

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

  利用介面

public class AppleFilter {

    public interface FilterApple{
        boolean filter(Apple apple);
    }

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

    public static class GreenAnd150WeightFilter implements FilterApple{
        @Override
        public boolean filter(Apple apple) {
            return (apple.getColor().equals("green")) && (apple.getWeight() >= 160);
} } 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 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", 150), new Apple("yellow", 120), new Apple("green", 170)); // List<Apple> appleList = findGreenApple(list); // assert appleList.size() == 2; // List<Apple> greenApples = findApple(list, "green"); // System.out.println(greenApples); List<Apple> apple = findApple(list, new GreenAnd150WeightFilter()); System.out.println(apple); List<Apple> yellowApples = findApple(list, new FilterApple() { @Override public boolean filter(Apple apple) { return "yellow".equals(apple.getColor()); } });
System.out.println(yellowApples); } }

  匿名內部類; 結果為: 5

public class MeaningOfThis {

    public final int value = 4;

    public void doIt() {
        int value = 6;
        Runnable r = new Runnable() {

            public final int value = 5;

            @Override
            public void run() {
                int value = 10;
                System.out.println(this.value);
            }
        };
        r.run();
    }

    public static void main(String[] args) {
        MeaningOfThis m = new MeaningOfThis();
        m.doIt();
    }
}

  lamda表示式

public class AppleFilter {

    public interface FilterApple{
        boolean filter(Apple apple);
    }

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

    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(new Apple("green", 150), new Apple("yellow", 120), new Apple("green", 170));

        //lambda表示式
        List<Apple> appleList = findApple(list, (Apple apple) -> {
            return apple.getColor().equals("green");
        });

        System.out.println(appleList);

    }

}