Java 8 行为参数化与Lambda

我们需要从一堆苹果中按颜色挑出符合要求的苹果。最初,我们可能会按不同的条件写几个相似的方法来做这件事

  • Version 1
public List<Apple> filterGreenApples(List<Apple> inventory){
    List<Apple> result = new ArrayList<>();
        for(Apple apple: inventory){
            if("green".equals(apple.getColor())){
                result.add(apple);
            }
        }
    return result;
}

public List<Apple> filterRedApples(List<Apple> inventory){
    List<Apple> result = new ArrayList<>();
        for(Apple apple: inventory){
            if("red".equals(apple.getColor())){
                result.add(apple);
            }
        }
    return result;
}

好像重复的代码有点太多了,只有颜色的判断条件不同而已,我们做一个重构

  • Version 2
public List<Apple> filterApplesByColor(List<Apple> inventory, String color){
    List<Apple> result = new ArrayList<>();
        for(Apple apple: inventory){
            if(apple.getColor().equals(color)){
                result.add(apple);
            }
        }
    return result;
}

单单颜色不能满足我们的需求,我们还希望按重量来过滤

  • Version 3
public List<Apple> filterApplesBiggerThanWeight(List<Apple> inventory, int weight){
    List<Apple> result = new ArrayList<>();
        for(Apple apple: inventory){
            if(apple.getWeight() > weight ){
                result.add(apple);
            }
        }
    return result;
}

颜色版本和重量版本差的只是判断条件,其他部分依然是相同的,同理对其他属性来说情况也是一样的,所以我们引入行为参数化。

  • Version 4
public interface ApplePredicate{
    public boolean test(Apple apple);
}
public List<Apple> filterApples(List<Apple> inventory, ApplePredicate p){
    List<Apple> result = new ArrayList<>();
        for(Apple apple: inventory){
            if(p.test() ){
                result.add(apple);
            }
        }
    return result;
}

public AppleGreenColorPredicate implements ApplePredicate{
    public boolean test(Apple apple){
        return "green".equals(apple.getColor());
    }
}
public AppleWeightPredicate implements ApplePredicate{
    public boolean test(Apple apple){
        return apple.getWeight() > 150; 
    }
}

行为参数化解决了我们的主要问题,但是实现看起来有点啰嗦,我们要实现AppleGreenColorPredicate,AppleWeightPredicate等等,有许多重复的类定义代码。Java 7时代我们使用匿名类来减少一部分代码

  • Version 5
List<Apple> redApples =  filterApples(inventory, new ApplePredicate() {
    public boolean test(Apple apple){
        return "red".equals(apple.getColor());
    }
}

到了Java 8我们可以使用Lambda来替代不易使用的匿名类

  • Version 6
List<Apple> redApples =  filterApples(inventory, apple -> "red".equals(apple.getColor()))

这样就简洁很多了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 一、Java 简介 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计...
    子非鱼_t_阅读 4,273评论 1 44
  • 明知虚荣会很虚,却还是依然虚荣着,明知小幸福才是最真实的然而还是放不下妄念, 还是依旧执着。明知自己不可能什么都去...
    浪客剑心_1fb6阅读 189评论 0 0
  • 慢旅行,放下所有,只为了给自己心一个机会,去寻找自我。走在异国风域的路上,闭上眼张开双臂,享受一种美妙的感觉。
    左拉不语阅读 205评论 0 0
  • 今天分享一下老师的名言。李笑来老师在“通往财富自由之路”中,有一期写到《为什么你一定要学会写作》。在专栏中,笑来老...
    一瑾会发光阅读 240评论 3 4