HashSet method removeIf() in Java
A HashSet method removeIf() used to remove all of the elements of this collection that satisfy the given predicate. It returns boolean value true, if this set object changed, otherwise false.
Syntax
 1 default boolean removeIf(Predicate<? super E> filter)
filter : It is a predicate which returns true for elements to be removed.
return : It returns boolean value true, if this set object changed, otherwise false.

Exceptions:

1. NullPointerException : if the specified filter is null2. UnsupportedOperationException : if the removeIf operation is not supported by this collection.
HashSet method removeIf()
 1 package iogyan.example;
 2 
 3 import java.util.Arrays;
 4 import java.util.HashSet;
 5 import java.util.List;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) {
 10 
 11         // create immutable list
 12         List<Integer> list = Arrays.asList(20, 10, 30, 40);
 13         HashSet<Integer> hashSet = new HashSet<>(list);
 14         System.out.println("A hash set elements : " + hashSet);
 15 
 16         // removeIf() with matching predicate
 17         boolean res1 = hashSet.removeIf(ele -> ele % 20 == 0);
 18         System.out.println("Does this set object changed : " + res1);
 19         System.out.println("A set elements : " + hashSet);
 20 
 21         // removeIf() with non-matching predicate
 22         boolean res2 = hashSet.removeIf(ele -> ele > 100);
 23         System.out.println("Does this set object changed : " + res2);
 24         System.out.println("A set elements : " + hashSet);
 25     }
 26 }
In the above example, a HashSet of type integer created by passing a list. A HashSet method removeIf() called by passing a predicate that determine whether an element value divisible by 20 and returns boolean value true, as this set object changed. A removeIf() method called by passing a predicate that does not match any element of this set. It returns boolean value false, as this set not changed.
Output
 1 A hash set elements : [20, 40, 10, 30]
 2 Does this set object changed : true
 3 A set elements : [10, 30]
 4 Does this set object changed : false
 5 A set elements : [10, 30]

HashSet method removeIf() with String

HashSet method removeIf() with String
 1 package iogyan.example;
 2 
 3 import java.util.Arrays;
 4 import java.util.HashSet;
 5 import java.util.List;
 6 
 7 public class StringMain {
 8 
 9     public static void main(String[] args) {
 10 
 11         // create immutable list
 12         List<String> list = Arrays.asList("Welcome", "to", "IOGyan");
 13         HashSet<String> hashSet = new HashSet<>(list);
 14         System.out.println("A hash set elements : " + hashSet);
 15 
 16         // removeIf() with matching predicate
 17         boolean res1 = hashSet.removeIf(str -> str.length() < 5);
 18         System.out.println("Does this set object changed : " + res1);
 19         System.out.println("A set elements : " + hashSet);
 20 
 21         // removeIf() with non-matching predicate
 22         boolean res2 = hashSet.removeIf(str -> str.startsWith("A"));
 23         System.out.println("Does this set object changed : " + res2);
 24         System.out.println("A set elements : " + hashSet);
 25     }
 26 }
In the above example, a HashSet of type String created by passing a list. A HashSet method removeIf() called by passing a predicate that determine whether an element string length less than 5 and returns boolean value true, as this set object changed. A removeIf() method called by passing a predicate that does not match any element of this set. It returns boolean value false, as this set not changed.
Output
 1 A hash set elements : [IOGyan, Welcome, to]
 2 Does this set object changed : true
 3 A set elements : [IOGyan, Welcome]
 4 Does this set object changed : false
 5 A set elements : [IOGyan, Welcome]

HashSet method removeIf() with custom object

HashSet method removeIf() with custom object
 1 package iogyan.example;
 2 
 3 import iogyan.model.Employee;
 4 
 5 import java.util.Arrays;
 6 import java.util.HashSet;
 7 import java.util.List;
 8 
 9 public class EmployeeMain {
 10 
 11     public static void main(String[] args) {
 12 
 13         Employee emp1 = new Employee(1, "Emp1","Tech", 1000);
 14         Employee emp2 = new Employee(2, "Emp2","Admin", 5000);
 15         Employee emp3 = new Employee(3, "Emp3","Admin", 12000);
 16 
 17         // create immutable list
 18         List<Employee> list = Arrays.asList(emp1, emp3, emp2);
 19         HashSet<Employee> hashSet = new HashSet<>(list);
 20         System.out.println("A hash set elements : " + hashSet);
 21 
 22         // removeIf() with matching predicate
 23         boolean res1 = hashSet.removeIf(emp -> emp.getSalary() < 5000);
 24         System.out.println("Does this set object changed : " + res1);
 25         System.out.println("A set elements : " + hashSet);
 26 
 27         // removeIf() with non-matching predicate
 28         boolean res2 = hashSet.removeIf(emp -> emp.getDept().equals("Tech"));
 29         System.out.println("Does this set object changed : " + res2);
 30         System.out.println("A set elements : " + hashSet);
 31     }
 32 }
In the above example, a HashSet of type Employee created by passing a list. A HashSet method removeIf() called by passing a predicate that determine whether an employee salary less than 5000 and returns boolean value true, as this set object changed. A removeIf() method called by passing a predicate that does not match any element of this set. It returns boolean value false, as this set not changed.
Output
 1 A hash set elements : [Emp3 Admin 12000.0, Emp1 Tech 1000.0, Emp2 Admin 5000.0]
 2 Does this set object changed : true
 3 A set elements : [Emp3 Admin 12000.0, Emp2 Admin 5000.0]
 4 Does this set object changed : false
 5 A set elements : [Emp3 Admin 12000.0, Emp2 Admin 5000.0]

Employee class

Employee class
 1 package iogyan.model;
 2 
 3 public class Employee implements Comparable<Employee> {
 4     private final int id;
 5     private final String name;
 6     private final String dept;
 7     private final double salary;
 8 
 9     public Employee(int id, String name, String dept, double salary) {
 10         this.id = id;
 11         this.name = name;
 12         this.dept = dept;
 13         this.salary = salary;
 14     }
 15 
 16     public int getId() {
 17         return this.id;
 18     }
 19 
 20     public String getName() {
 21         return this.name;
 22     }
 23 
 24     public String getDept() {
 25         return this.dept;
 26     }
 27 
 28     public double getSalary() {
 29         return this.salary;
 30     }
 31 
 32     @Override
 33     public int hashCode() {
 34         return super.hashCode();
 35     }
 36 
 37     @Override
 38     public boolean equals(Object obj) {
 39         return this.id == ((Employee)obj).id;
 40     }
 41 
 42     @Override
 43     public String toString() {
 44         return this.getName() + " " + this.getDept() + " " + this.getSalary();
 45     }
 46 
 47     @Override
 48     public int compareTo(Employee o) {
 49         return this.getId() - o.getId();
 50     }
 51 }
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us