博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
行为参数化
阅读量:6953 次
发布时间:2019-06-27

本文共 3899 字,大约阅读时间需要 12 分钟。

假设有如下业务:有一堆有颜色和重量的苹果,我需要通过颜色和重量取出相应苹果 定义苹果

public class Apple {        private int weight = 0;        private String color = "";        public Apple(int weight, String color){            this.weight = weight;            this.color = color;        }        public Integer getWeight() {            return weight;        }        public void setWeight(Integer weight) {            this.weight = weight;        }        public String getColor() {            return color;        }        public void setColor(String color) {            this.color = color;        }        public String toString() {            return "Apple{" +                    "color='" + color + '\'' +                    ", weight=" + weight +                    '}';        }    }复制代码

假设

inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red"));复制代码

解决方案1:

List
result = new ArrayList<>(); for(Apple apple: inventory){ if("green".equals(apple.getColor())){ result.add(apple); } }复制代码

这是最常见的方法。但是这样的结构很难复用。比如我颜色不确定呢?

解决方案2:

List
result = new ArrayList<>(); for(Apple apple: inventory){ if(apple.getColor().equals(color)){ result.add(apple); } }复制代码

如果我需要100g以上的且红色的苹果我就需要

List
result = new ArrayList<>(); for(Apple apple: inventory){ if(apple.getColor().equals(color) && apple.getWeight() > weight){ result.add(apple); } }复制代码

如果我需要100g以上或者红色的苹果

List
result = new ArrayList<>(); for(Apple apple: inventory){ if(apple.getColor().equals(color) || apple.getWeight() > weight){ result.add(apple); } }复制代码

是不是变得没完没了了? 解决方案3:

public static List
filterApples(List
inventory, ApplePredicate p){ List
result = new ArrayList<>(); for(Apple apple : inventory){ if(p.test(apple)){ result.add(apple); } } return result; }复制代码
interface ApplePredicate{        boolean test(Apple a);    }     class AppleWeightPredicate implements ApplePredicate{        public boolean test(Apple apple){            return apple.getWeight() > 150;        }    }     class AppleColorPredicate implements ApplePredicate{        public boolean test(Apple apple){            return "green".equals(apple.getColor());        }    }     class AppleRedAndHeavyPredicate implements ApplePredicate{        public boolean test(Apple apple){            return "red".equals(apple.getColor())                    && apple.getWeight() > 150;        }    }复制代码
List
greenApples2 = filterApples(inventory, new AppleColorPredicate());复制代码

这种方法和合适。不过如果规则也是不确定的呢?

解决方案4:

List
redApples2 = filterApples(inventory, new ApplePredicate() { public boolean test(Apple a){ return a.getColor().equals("red"); } });复制代码

Good!这样就能做到定制化了。不过通过lambda写起来更加优美

解决方案5:

List
redApples2 = filterApples(inventory, (Apple a)-> a.getColor().equals("red")); 复制代码

如果我们要推广。不只是苹果而是所有的判断规则?

解决方案6:

interface Predicate
{ boolean test(T t); } public static
List
filter(List
inventory, Predicate
p){ List
result = new ArrayList<>(); for(T apple : inventory){ if(p.test(apple)){ result.add(apple); } } return result; } 复制代码
List
redApples2 = filter(inventory, (Apple a)-> a.getColor().equals("red"));复制代码

其实java 8 的思路也是这样的 解决方案7:

List
redApples2 = inventory .stream() .filter((Apple a)-> a.getColor().equals("red")) .collect(Collectors.toList());复制代码

转载地址:http://sbnil.baihongyu.com/

你可能感兴趣的文章
使用docker镜像玩转steam挂卡
查看>>
激发个人潜能凝聚团队力量——九八七酒业户外拓展活动圆满成功
查看>>
修改root密码方式及克隆虚拟机
查看>>
hadoop技术入门学习之发行版选择
查看>>
thinkphp简介
查看>>
nodejs RSA 与 jsencrypt实现前端加密后端解密功能
查看>>
学习笔记sql server数据库批量查询和删除内容执行语句
查看>>
SpringBoot同时集成Guava和Redis作为缓存
查看>>
(二)Java B2B2C o2o多用户商城 springcloud架构-服务消费者(rest+ribbon)
查看>>
微服务的断路器实现图解Golang通用版
查看>>
3. window 上安装redis(单机版) 及客户端
查看>>
Hadoop日志存放路径详解
查看>>
Python如何操作文件?Python基础教程,第十二讲,文件读写
查看>>
Lync 2013企业实战(七)
查看>>
Windows命令行杀死占用端口的进程
查看>>
我的友情链接
查看>>
Git配置了秘钥认证后,tortoisegit仍需要认证的问题
查看>>
Linux文件rwx属性的含义
查看>>
Sql Loader的简单使用
查看>>
bash脚本编程之二 条件判断
查看>>