First let’s see an instance:
public static void major(String[] args) {
Predicate<Integer> predicate = n -> (n < 20);
System.out.println(predicate.check(10));
}
Right here, we ship to Predicate an integer worth of 10. Additionally, we offer the
logic that the argument quantity which is represented by n needs to be much less
than 20 to be true in any other case it returns false. So clearly the Boolean
worth right here is true. Let’s see the output:
true
Listed below are extra examples:
public static void major(String[] args) {
Predicate<Integer> predicate1 = n -> (n > 20);
System.out.println(predicate1.check(10));
Predicate<Integer> predicate2 = n -> (n == 10);
System.out.println(predicate2.check(5));
Predicate<Integer> predicate3 = n -> (n < 10);
System.out.println(predicate3.check(2));
}
With the output:
false
false
true
Within the above examples, we’re utilizing the Predicate by the tactic
check(). Typically, Predicate has 5 strategies: check() , isEqual() , and()
, or() , negate() :
a- check(): it returns true if the argument matches the situation in any other case
false.
b- isEqual: returns true if two arguments are equal in response to object’s equals()
methodology in any other case false.
c- and(): It’s used when there are a number of logic situations which might be
checked by the Predicate, it returns true provided that all situations are true
in any other case false.
d- or(): It’s used when
there are a number of logic situations which might be checked by the Predicate, it
returns true if one of many situations is true. If all of the situations are
false then it returns false.
e- negate(): It’s opposite to check() methodology. It returns false if the
argument matches the situation or returns true if the argument doesn’t
match the situation
We noticed an instance for check(), let’s present the instance of different
strategies:
v isEqual():
public static void major(String[] args) {Predicate<String> predic = Predicate.isEqual(
"Hey World");
System.out.println(predic.check("Hey World"));
System.out.println(predic.check("Hey"));
}
Output:
true
false
v and():
public static void major(String[] args) {
Predicate<Integer> predicate1 = n -> (n > 20);
Predicate<Integer> predicate2 = n -> (n == 25);Predicate<Integer> andPredicate = predicate1.and(predicate2);
System.out.println(andPredicate.check(30));
System.out.println(andPredicate.check(25));}
Output:
false
true
v or():
public static void major(String[] args) {Predicate<Integer> predicate1 = n -> (n >
20);
Predicate<Integer> predicate2 = n -> (n == 25);Predicate<Integer> andPredicate = predicate1.or(predicate2);
System.out.println(andPredicate.check(30));
System.out.println(andPredicate.check(25));
System.out.println(andPredicate.check(15));}
Output:
true
true
false
v negate():
public static void major(String[] args) {Predicate<Integer> predicate1 = i -> i >
15;Predicate<Integer> negate = predicate1.negate();
System.out.println(negate.check(30));
System.out.println(negate.check(10));}
Output:
false
true