zoukankan      html  css  js  c++  java
  • Java8新特性——Lambda表达式-1

    一、抛出需求

      超市中挑选苹果,挑选条件多样化。

      示例:找出绿色并且重量等于150的苹果,找出红色并且重量小于120苹果。

    1、苹果类

    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;
        }
    }

    二、实现方案

      采用策略模式,实现数据筛选。

    1、查找苹果类

    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、实现方法

    • 方法一、继承扩展接口实现多个filter

      • 绿色并且重量等于150的苹果filter
    public class GreenAnd150WeightFilter implements AppleFilter{
    
        @Override
        public boolean filter(Apple apple) {
            return ("green".equals(apple.getColor()) && 150 == apple.getWeight());
        }
    
    }
      • 红色并且重量小于120苹果 filter
    public class RedLess120WeightFilter implements AppleFilter {
        
        @Override
        public boolean filter(Apple apple) {
            return ("red".equals(apple.getColor()) && 120 > apple.getWeight());
        }
    }
      • 查询实现与结果
    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); }

    •  方法二、匿名内部类

    public static void main(String[] args) {
            List<Apple> appleList = Arrays.asList(new Apple("green", 150), new Apple("red",100));
    
            //查找绿色并且重量等于150的苹果
            List<Apple> greenApples = findApple(appleList, new AppleFilter() {
                @Override
                public boolean filter(Apple apple) {
                    return ("green".equals(apple.getColor()) && 150 == apple.getWeight());
                }
            });
            System.out.println(greenApples);
            //查找红色并且重量小于120苹果 
            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、小结

      策略模式的两种实现方法,继承实现多个filter类、匿名内部类,可以方便实现复杂条件的数据筛选,但是在代码上显得有些累赘。

      

  • 相关阅读:
    文本比较算法Ⅴ——回顾贴,对前面几篇文章的回顾与质疑
    键盘监控的实现Ⅱ——容易产生误解的CallNextHookEx函数
    利用WPF建立自适应窗口大小布局的WinForm窗口
    计算机中的颜色XIII——颜色转换的快速计算公式
    键盘监控的实现Ⅲ——按键消息的修改(映射)
    计算机中的颜色XI——从色相值到纯色的快速计算(新的公式)
    Dot Net中InputLanguage对象的使用限制
    计算机中的颜色XII——快速计算纯色的色相值(新的公式)
    关于房产中介网的设计随想
    java笔记:熟练掌握线程技术基础篇之解决资源共享的问题(中)前篇
  • 原文地址:https://www.cnblogs.com/gavincoder/p/11788024.html
Copyright © 2011-2022 走看看