What is Predicate?Java 8 comes with a concept of
functional interface. An interface having only one abstract method and any number of default and static methods is called a
functional interface.
A
Predicate (boolean-valued function) of one argument which is present in java.util.function package. It is predefined functional interfaces that It has only one abstract method i.e, test().
1 2 3 | interface Predicate<T> { public boolean test(T t); }
|
Predicate Example to check age is greater than 18 or not.
Step: -11 2 3 4 5 6 7 | public boolean test(Integer age) { if (age > 18) { return true; } else { return false; } }
|
Step -21 2 3 4 5 6 7 8 9 | public class PredicateExample {
public static void main(String[] args) { Predicate<Integer> p = age -> (age > 18); System.out.println(p.test(21)); // true System.out.println(p.test(17)); // false }
}
|
Predicate Example to check collection is empty or not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.blogspot.ekumeedkiasha;
import java.util.ArrayList; import java.util.Collection; import java.util.function.Predicate;
public class PredicateExample {
public static void main(String[] args) { Predicate<Collection> p = c -> c.isEmpty();
ArrayList<Object> al = new ArrayList<>(); al.add("Monday");
ArrayList<Object> al1 = new ArrayList<>();
System.out.println(p.test(al)); // false System.out.println(p.test(al1)); // true }
}
|
4 comments
Write commentskindly also make videos on spring cloud and microservices
Replykindly also make videos on spring cloud and microservices
ReplySure will do that in the mean time please checkout my spring boot tutorials
Replyhttps://www.youtube.com/watch?v=ou15p7-HkxA&list=PLJc-LD5TzDQToG2MOYDAuCl-JFZoMBxgC
Sure will do that in the mean time please checkout my spring boot tutorials
Replyhttps://www.youtube.com/watch?v=ou15p7-HkxA&list=PLJc-LD5TzDQToG2MOYDAuCl-JFZoMBxgC
EmoticonEmoticon