In any SQL statement, 'where' condition is always referred to as "Predicate". Predicate is a function in java to apply a filter on it.
Introduced in Java 1.8, Predicate is a function or interface which has a
method that returns boolean. It has got other logical methods like 'and',
'or', 'negate', 'isEquals'. Here our point of interest will be the 'test'
method which returns boolean. The method 'test' will be applied with Lambda
Expression.
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class FilterFramework {
public static <T>
List<T> filter(List<T> items, Predicate<T> p) {
List<T> resultList = new
ArrayList<T>();
for (T t : items) {//
<-------------- This can be re-written
if (p.test(t)) {// <-------------
to implement stream
//<-------------instead of Iterating
resultList.add(t);
}
}
return resultList;
}
}
Thank you for sharing such a useful article. It will be useful to those who are looking for knowledge. Continue to share your knowledge with others through posts like these, and keep posting on
ReplyDeleteData Engineering Services
Advanced Data Analytics Solutions
Data Modernization Services
AI & ML Service Provider