Java8 new features --Lambda expression -1

First, throw demand

  Pick apples in the supermarket, diversified selection conditions.

  Example: to identify and green apple weight equal to 150, and the weight is less than 120 to identify red apples.

1, Apple class

public class Apple {

    private String color;
    private int weight;

    public Apple(String color, int weight) {
        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;
    }

    @Override
    public String toString() {
        return "color=" + this.color + ",weight=" + this.weight;
    }
}

 

Second, implementation

  Adopt strategies mode, data filtering.

1, look like apples

public class FindApple {

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

2, Implementation

  • A method, inheritance expansion interface to achieve multiple filter

    • Green apple and weight equal to the filter 150
public class GreenAnd150WeightFilter implements AppleFilter{

    @Override
    public boolean filter(Apple apple) {
        return ("green".equals(apple.getColor()) && 150 == apple.getWeight());
    }

}
    • Apple red filter and the weight is less than 120
public class RedLess120WeightFilter implements AppleFilter {
    
    @Override
    public boolean filter(Apple apple) {
        return ("red".equals(apple.getColor()) && 120 > apple.getWeight());
    }
}
    • Implementation and results of the query
public static void main(String[] args) {
        List<Apple> appleList = Arrays.asList(new Apple("green", 150), new Apple("red",100));
List
<Apple> greenApples = findApple(appleList, new GreenAnd150WeightFilter()); System.out.println(greenApples); List<Apple> redApples = findApple(appleList, new RedLess120WeightFilter()); System.out.println(redApples); }

 

  •  Method two anonymous inner classes

public  static  void main (String [] args) { 
        List <the Apple> appleList = Arrays.asList ( new new the Apple ( "Green", 150), new new the Apple ( "Red", 100 )); 

        // Find green weight equal to 150 and Apple 
        List <the Apple> greenApples = findApple (appleList, new new AppleFilter () { 
            @Override 
            public  Boolean filter (the Apple Apple) {
                 return ( "Green" .equals (apple.getColor ()) == 150 && apple.getWeight () ); 
            } 
        }); 
        System.out.println (greenApples); 
        // Find weight of less than 120 and red apple
        List<Apple> redApples = findApple(appleList, new AppleFilter() {    
            @Override
            public boolean filter(Apple apple) {
                return ("red".equals(apple.getColor()) && 120 > apple.getWeight());
            }
        });
        System.out.println(redApples);
    }

 3 Summary

  Two strategies modes of implementation, implement multiple inheritance filter classes, anonymous inner classes, you can easily implement complex data filtering conditions, but in the code a bit cumbersome.

  

 

Guess you like

Origin www.cnblogs.com/gavincoder/p/11788024.html