# Filter

In 
Published 2022-12-03

This tutorial explains to you the design pattern named Filter (which is a Structural Pattern).

# Filter Pattern - theory

Filter Design Pattern in Java is used when we want to apply simple or complex filters. We can create filter combinations (AND, OR, etc) on a list of objects.

Take a look at the following UML diagram representing the Filter design pattern (for my example):

# Filter Pattern - example

Here is that example using the Filter Design Pattern in Java:

package filter.java.pattern.example;
 
public class Employee {
 
       private String name;
       private String job;
       private String gender;
 
       public Employee(String name, String job, String gender){
          this.name = name;
          this.job = job;
          this.gender = gender;     
       }
 
       public String getName() {
          return name;
       }
        
       public String getJob() {
          return job;
       }
        
       public String getGender() {
              return gender;
       }
}
package filter.java.pattern.example;
 
import java.util.List;
 
public interface Filter {
 
    public List<Employee> applyFilter(List<Employee> employee);
     
}
package filter.java.pattern.example;
 
import java.util.ArrayList;
import java.util.List;
 
public class IsAnalyst implements Filter {
     
    @Override
    public List<Employee> applyFilter(List<Employee> employees) {
         
        List<Employee> analystsList = new ArrayList<Employee>(); 
           
        for (Employee emp : employees) {
           if(emp.getJob().equalsIgnoreCase("Analyst")){
               analystsList.add(emp);
           }
        }
         
        return analystsList;
    }
}
package filter.java.pattern.example;
 
import java.util.ArrayList;
import java.util.List;
 
public class IsMale implements Filter {
     
    @Override
    public List<Employee> applyFilter(List<Employee> employees) {
         
        List<Employee> maleList = new ArrayList<Employee>(); 
           
        for (Employee emp : employees) {
           if(emp.getGender().equalsIgnoreCase("Male")){
               maleList.add(emp);
           }
        }
         
        return maleList;
    }
}
package filter.java.pattern.example;
 
import java.util.ArrayList;
import java.util.List;
 
public class IsFemale implements Filter {
     
    @Override
    public List<Employee> applyFilter(List<Employee> employees) {
         
        List<Employee> femaleList = new ArrayList<Employee>(); 
           
        for (Employee emp : employees) {
           if(emp.getGender().equalsIgnoreCase("Female")){
               femaleList.add(emp);
           }
        }
         
        return femaleList;
    }
}
package filter.java.pattern.example;
 
import java.util.ArrayList;
import java.util.List;
 
public class AndFilterOperation implements Filter {
     
    private Filter filter1;
    private Filter filter2;
 
       public AndFilterOperation(Filter filter1, Filter filter2) {
          this.filter1 = filter1;
          this.filter2 = filter2; 
       }
 
       @Override
       public List<Employee> applyFilter(List<Employee> employees) {
        
          List<Employee> andFilterResult = new ArrayList<Employee>();
           
          andFilterResult = filter1.applyFilter(filter2.applyFilter(employees));
          
          return andFilterResult;
           
       }
 
}
package filter.java.pattern.example;
 
import java.util.ArrayList;
import java.util.List;
 
public class OrFilterOperation implements Filter {
     
    private Filter filter1;
    private Filter filter2;
 
       public OrFilterOperation(Filter filter1, Filter filter2) {
          this.filter1 = filter1;
          this.filter2 = filter2; 
       }
 
       @Override
       public List<Employee> applyFilter(List<Employee> employees) {
        
          List<Employee> orFilterResult = new ArrayList<Employee>();
           
          List<Employee> list1 = filter1.applyFilter(employees);
          List<Employee> list2 = filter2.applyFilter(employees);
           
          orFilterResult = list1;
           
          for (Employee emp : list2) {
                 if(!orFilterResult.contains(emp)){
                     orFilterResult.add(emp);
                 }
              } 
  
          return orFilterResult;
       }
}
package filter.java.pattern.example;
 
import java.util.ArrayList;
import java.util.List;
 
public class FilterJavaPatternExample {
 
    public static void main(String[] args) {
     
        List<Employee> empList = new ArrayList<Employee>();
 
        empList.add(new Employee("Dan", "Analyst","Male"));
        empList.add(new Employee("Emilie", "Analyst", "Female"));
        empList.add(new Employee("Mike", "Analyst", "Male"));
        empList.add(new Employee("Dana", "Manager", "Female"));
        empList.add(new Employee("Elthon", "Analyst", "Male"));
        empList.add(new Employee("George", "Manager", "Male"));
        empList.add(new Employee("Ely", "Clerk", "Female"));
 
        Filter male = new IsMale();
        System.out.println("The list of Males: ");
        printList(male.applyFilter(empList));
         
        Filter female = new IsFemale();
        System.out.println("\nThe list of Females: ");
        printList(female.applyFilter(empList));
         
        Filter analyst = new IsAnalyst();
        System.out.println("\nThe list of Analysts: ");
        printList(analyst.applyFilter(empList));
        
        Filter maleAndAnalyst = new AndFilterOperation(male, analyst);
        System.out.println("\nThe list of Males & Analysts: ");
        printList(maleAndAnalyst.applyFilter(empList));
         
        Filter maleOrAnalyst = new OrFilterOperation(male, analyst);
        System.out.println("\nThe list of Males OR Analysts: ");
        printList(maleOrAnalyst.applyFilter(empList));
         
       }
     
    public static void printList(List<Employee> empList){
            
        for (Employee emp : empList) {
            System.out.println("Employee :: Name= " + emp.getName() + ", Job= "+emp.getJob()+ ", Gender= " + emp.getGender() );
        }
    }
}

When you run this example you will receive the following result: