Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

java.util.

function
Interface Predicate<T>
It is a functional interface which represents a predicate (boolean-valued function) of one argument. It is defined
in the java.util.function package and contains test() a functional method.

import java.util.function.Predicate;
public class Test
{
public static void main(String[] args)
{
Predicate<Integer> pr = a -> (a > 18);
System.out.println(pr.test(10));
}
}
import java.util.function.Predicate;
public class Test
{
static Boolean checkAge(int age)
{
if(age>17)
return true;
else return false;
}

public static void main(String[] args)


{
Predicate<Integer> predicate = Test::checkAge;
boolean result = predicate.test(25);
System.out.println(result);
}
}

import java.util.function.Predicate;
public class Test
{
public static void main(String[] args)
{
Predicate<Integer> greaterThanTen = (i) -> i > 10;
Predicate<Integer> lowerThanTwenty = (i) -> i < 20;
boolean result = greaterThanTen.and(lowerThanTwenty).test(15);
System.out.println(result);
boolean result2 = greaterThanTen.and(lowerThanTwenty).negate().test(15);
System.out.println(result2);
}
}

import java.util.function.Predicate;
class Test
{
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);
}
}

You might also like