过滤器
实现功能: 可像安装插件一样的对实体类对象进行筛选
package com.starcharge.evcs.customization;
import java.util.ArrayList;
import java.util.List;
/**
- @Date: 2019/4/12
- @Description:
*/
//过滤器
public interface CatFilter {
boolean accept(Cat cat);
}
class Excute {
public static List
List
cats.forEach((cat) -> {
if (catFilter.accept(cat)) {
filterCat.add(cat);
}
});
return filterCat;
}
public static void main(String[] args) {
Cat cat1 = new Cat(1, "RED");
Cat cat2 = new Cat(2, "RED");
Cat cat3 = new Cat(3, "RED");
Cat cat4 = new Cat(4, "eED");
Cat cat5 = new Cat(5, "1ED");
List
cats.add(cat1);
cats.add(cat2);
cats.add(cat3);
cats.add(cat4);
cats.add(cat5);
List
@Override
public boolean accept(Cat cat) {
return "RED".equalsIgnoreCase(cat.getColor());
}
});
System.out.println(resultCats);
}
}
//实体类
class Cat {
private int id;
private String color;
private String weight;
public Cat(int id, String color) {
this.id = id;
this.color = color;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Cat{");
sb.append("id=").append(id);
sb.append(", color='").append(color).append(''');
sb.append(", weight='").append(weight).append(''');
sb.append('}');
return sb.toString();
}
}