Monday, September 30, 2019

Java 8 – Collection.removeIf method

Conditional removal from a Collection before Java 8

Java 7 code showing Collection handling using Iterator

//Employee.java(POJO class)
package com.javabrahman.java8;
public class Employee {
  private String name;
  private Integer age;
  private Double salary;
  public Employee(String name, Integer age, Double salary) {
    this.name = name;
    this.age = age;
    this.salary = salary;
  }
  public String toString(){
    return "Employee Name:"+this.name
        +"  Age:"+this.age
        +"  Salary:"+this.salary;
  }
//getters and setters for name, age and salary go here
//standard equals() and hashcode() code go here
}



//CollectionRemoveIfExample.java

com.javabrahman.java8.collections;
import com.javabrahman.java8.Employee;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CollectionRemoveIfExample {
  static List<Employee> employeeList = new ArrayList<>();
  public static void main(String[] args) {
    employeeList.add(new Employee("Tom Jones", 45, 7000.00));
    employeeList.add(new Employee("Harry Major", 25, 10000.00));
    employeeList.add(new Employee("Ethan Hardy", 65, 8000.00));
    employeeList.add(new Employee("Nancy Smith", 22, 12000.00));
    employeeList.add(new Employee("Deborah Sprightly", 29, 9000.00));
    for(Iterator empIterator=employeeList.iterator();
        empIterator.hasNext();) {
      Employee emp = empIterator.next();
      if(emp.getAge() > 30){
        empIterator.remove();
      }
    }
    System.out.println("Employees below the age of 30");
        employeeList.forEach(System.out::println);
  }
}

 OUTPUT of the above code

Employees below the age of 30
Employee Name: Harry Major   Age:25   Salary:10000.0
Employee Name: Nancy Smith   Age:22   Salary:12000.0
Employee Name: Deborah Sprightly   Age:29   Salary:9000.0

Conditional Removal from a Collection using Java 8’s Collection.removeIf() default method

Java 8’s java.util.Collection interface has a new default method added to it named removeIf(). This method is defined with the following signature –

default boolean removeIf(Predicate<? super E> filter)
Where,

     – filter is an instance of a Predicate Functional Interface.


Java 8 code showing Collection.removeIf() usage
public class CollectionRemoveIfExample {
  static List<Employee> employeeList = new ArrayList<>();
  public static void main(String[] args) {
    employeeList.add(new Employee("Tom Jones", 45, 7000.00));
    employeeList.add(new Employee("Harry Major", 25, 10000.00));
    employeeList.add(new Employee("Ethan Hardy", 65, 8000.00));
    employeeList.add(new Employee("Nancy Smith", 22, 12000.00));
    employeeList.add(new Employee("Deborah Sprightly", 29, 9000.00));
    employeeList.removeIf((Employee emp) -> emp.getAge() > = 30);
    System.out.println("Employees below the age of 30");
    employeeList.forEach(System.out::println);
  }
}
 OUTPUT of the above code
Employees below the age of 30
Employee Name: Harry Major   Age:25   Salary:10000.0
Employee Name: Nancy Smith   Age:22   Salary:12000.0
Employee Name: Deborah Sprightly   Age:29   Salary:9000.0


O(n2) Vs O(n) – Big performance improvement

Removal from an ArrayList using an iterator has time complexity of O(n2), while the same operation when performed using Java 8’s new Collection.removeIf() method has a complexity of O(n). This is a significant improvement in performance. If an application has large-sized ArrayLists then using the Collection.removeIf() method will result in the application being speeded up by an order of complexity, making the choice of the new method over the earlier one a no-brainer in such scenarios.




No comments:

Post a Comment