predicado
something which is affirmed or denied concerning an argument of a proposition.
Markhor
something which is affirmed or denied concerning an argument of a proposition.
// Java program to illustrate Predicate Chaining
import java.util.function.Predicate;
public class PredicateInterfaceExample2 {
public static void main(String[] args)
{
Predicate<Integer> greaterThanTen = (i) -> i > 10;
// Creating predicate
Predicate<Integer> lowerThanTwenty = (i) -> i < 20;
boolean result = greaterThanTen.and(lowerThanTwenty).test(15);
System.out.println(result);
// Calling Predicate method
boolean result2 = greaterThanTen.and(lowerThanTwenty).negate().test(15);
System.out.println(result2);
}
}
// Java program to illustrate OR Predicate
import java.util.function.Predicate;
class PredicateInterfaceExample4 {
public static Predicate<String> hasLengthOf10 = new Predicate<String>() {
@Override
public boolean test(String t)
{
return t.length() > 10;
}
};
public static void predicate_or()
{
Predicate<String> containsLetterA = p -> p.contains("A");
String containsA = "And";
boolean outcome = hasLengthOf10.or(containsLetterA).test(containsA);
System.out.println(outcome);
}
public static void main(String[] args)
{
predicate_or();
}
}
// Java program to illustrate Simple Predicate
import java.util.function.Predicate;
public class PredicateInterfaceExample1 {
public static void main(String[] args)
{
// Creating predicate
Predicate<Integer> lesserthan = i -> (i < 18);
// Calling Predicate method
System.out.println(lesserthan.test(10));
}
}
// Java program to illustrate
// passing Predicate into function
import java.util.function.Predicate;
class PredicateInterfaceExample3 {
static void pred(int number, Predicate<Integer> predicate)
{
if (predicate.test(number)) {
System.out.println("Number " + number);
}
}
public static void main(String[] args)
{
pred(10, (i) -> i > 7);
}
}
// Java program to illustrate
// negate Predicate
import java.util.function.Predicate;
class PredicateInterfaceExample6 {
public static Predicate<String> hasLengthOf10 = new Predicate<String>() {
@Override
public boolean test(String t)
{
return t.length() > 10;
}
};
public static void predicate_negate()
{
String lengthGTThan10 = "Thunderstruck is a 2012 children's "
+ "film starring Kevin Durant";
boolean outcome = hasLengthOf10.negate().test(lengthGTThan10);
System.out.println(outcome);
}
public static void main(String[] args)
{
predicate_negate();
}
}
// Java program to demonstrate working of predicates
// on collection. The program finds all admins in an
// arrayList of users.
import java.util.function.Predicate;
import java.util.*;
class User
{
String name, role;
User(String a, String b) {
name = a;
role = b;
}
String getRole() { return role; }
String getName() { return name; }
public String toString() {
return "User Name : " + name + ", Role :" + role;
}
public static void main(String args[])
{
List<User> users = new ArrayList<User>();
users.add(new User("John", "admin"));
users.add(new User("Peter", "member"));
List admins = process(users, (User u) -> u.getRole().equals("admin"));
System.out.println(admins);
}
public static List<User> process(List<User> users,
Predicate<User> predicate)
{
List<User> result = new ArrayList<User>();
for (User user: users)
if (predicate.test(user))
result.add(user);
return result;
}
}