集合筛选
场景
条件过滤,条件组合
代码
Sample1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| interface Criteria { List<NumberData> filter(List<NumberData> list); }
public class AndCriteria implements Criteria{ private Criteria a; private Criteria b;
public AndCriteria(Criteria a, Criteria b) { this.a = a; this.b = b; }
@Override public List<NumberData> filter(List<NumberData> list){ return b.filter(a.filter(list)); } }
class LessThan5 implements Criteria { public List<NumberData> filter(List<NumberData> list) { return list.stream().filter(i -> i.getVal() < 5).collect(Collectors.toList()); } }
class LargerThan10 implements Criteria { public List<NumberData> filter(List<NumberData> list) { return list.stream().filter(i -> i.getVal() > 5).collect(Collectors.toList()); } }
|
特点
和组合模式思路有点相似,组合后依然实现相同接口,统一建模