Java 8 filter method Example

7 years ago Lalit Bhagtani 0

In this tutorial, we will learn about Stream java 8 filter method.

java 8 filter

filter() method :-

This method is called on Stream object, it takes one Predicate as an argument and returns a new stream consisting of the elements of the called stream that match the given predicate.

Syntax :-  Stream<T> filter(Predicate<? super T> predicate)

Reference :-  filter() method JavaDocs 

Problem Statement :-

You have given list of employees, find out all the employees whose designation is “Manager” and age is above 30.

Employee Bean :-

  public class Employee {

    private long empId;
    private String name;
    private int age;
    private String designation;
 
    public Employee(long empId, String name, int age, String designation) {
      super();
      this.empId = empId;
      this.name = name;
      this.age = age;
      this.designation = designation;
    }
 
    public long getEmpId() {
      return empId;
    }
    public void setEmpId(long empId) {
      this.empId = empId;
    }
    public String getName() {
      return name;
    }
    public void setName(String name) {
      this.name = name;
    }
    public int getAge() {
      return age;
    }
    public void setAge(int age) {
      this.age = age;
    }
    public String getDesignation() {
      return designation;
    }
    public void setDesignation(String designation) {
      this.designation = designation;
    }
 
  }

Java 7 :- To implement above problem, you first iterate through the list of employees, check designation and age of each employee object and if it match the given criteria store it in new list.


  public static void main(String[] args){ 
    List<Employee> empList = new ArrayList<Employee>();
    empList.add(new Employee(1,"Robert",35,"Manager"));
    empList.add(new Employee(2,"Martin",35,"General Manager"));
    empList.add(new Employee(1,"Jack",25,"Manager"));
    empList.add(new Employee(3,"Akshay",20,"Developer"));
    empList.add(new Employee(1,"Arvind",40,"Manager"));
    empList.add(new Employee(1,"Dinesh",45,"Manager"));
 
    List<Employee> tempList = new ArrayList<Employee>();
 
    for(Employee e : empList){
      if("Manager".equalsIgnoreCase(e.getDesignation()) && e.getAge() > 30){
        tempList.add(e);
      }
    }
 
    for(Employee e : tempList){
      System.out.println("Designation :- " + e.getDesignation() + " , Age :- " + e.getAge());
    }
  }

Java 8 filter :- To implement this, we will use collect() and forEach() method of Stream API.


  public static void main(String[] args){		
    List<Employee> empList = new ArrayList<Employee>();
    empList.add(new Employee(1,"Robert",35,"Manager"));
    empList.add(new Employee(2,"Martin",35,"General Manager"));
    empList.add(new Employee(1,"Jack",25,"Manager"));
    empList.add(new Employee(3,"Akshay",20,"Developer"));
    empList.add(new Employee(1,"Arvind",40,"Manager"));
    empList.add(new Employee(1,"Dinesh",45,"Manager"));
		
    List<Employee> tempList = empList.stream()
				.filter(e -> "Manager".equalsIgnoreCase(e.getDesignation()) && e.getAge() > 30)
				.collect(Collectors.toList());
	
    tempList.forEach(e -> System.out.println("Designation :- " + e.getDesignation() + " , Age :- " + e.getAge()));		
  }

Result :- 

Designation :- Manager , Age :- 35
Designation :- Manager , Age :- 40
Designation :- Manager , Age :- 45

That’s all for Java 8 filter method Example. If you liked it, please share your thoughts in comments section and share it with others too.