tomatoes

天行健,君子以自强不息;地势坤,君子以厚德载物

导航

插件-过滤器

过滤器

实现功能: 可像安装插件一样的对实体类对象进行筛选

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 filterCatByCatFilter(List cats, CatFilter catFilter) {
​ List filterCat = new ArrayList<>();
​ 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 = new ArrayList<>();
​ cats.add(cat1);
​ cats.add(cat2);
​ cats.add(cat3);
​ cats.add(cat4);
​ cats.add(cat5);
​ List resultCats = filterCatByCatFilter(cats, new CatFilter() {
​ @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();
​ }
}

posted on 2019-04-19 16:29  zongJianKun  阅读(184)  评论(0编辑  收藏  举报