• 插件-过滤器


    过滤器

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

    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();
    ​ }
    }

  • 相关阅读:
    The executable was signed with invalid entitlements
    iOS7 文本转语音 AVSpeechSynthesizer
    The document "ViewController.xib" could not be opened. Could not read archive.
    UIPanGestureRecognizer
    UINavigationController
    IE6下面的css调试工具
    15款最好的网站音乐播放器
    ASP.NET常用加密解密方法
    ASP.NET根据IP获取省市地址
    强悍的CSS工具组合:Blueprint, Sass, Compass
  • 原文地址:https://www.cnblogs.com/zongJianKun/p/10736754.html
Copyright © 2020-2023  润新知