Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Predicate in Java

I am going through the code which uses Predicate in Java. I have never used Predicate. Can someone guide me to any tutorial or conceptual explanation of Predicate and its implementation in Java?

like image 618
srikanth Avatar asked Jun 02 '10 04:06

srikanth


People also ask

Which is true for a predicate in Java?

Predicate in general meaning is a statement about something that is either true or false. In programming, predicates represent single argument functions that return a boolean value.

Why do we use predicates?

You use a Predicate (javadoc) when you need a function that takes one argument and returns a boolean . For example, you can use a Predicate in a situation where you want to filter a stream to remove elements that don't satisfy some logical condition, or find the first element that satisfies the condition.

What is a predicate in OOP?

Predicate classes extend the standard object-oriented modelling constructs by reifying transient states or behavior modes of objects. A predicate class has all the properties of a normal class, including a name, a set of superclasses, a set of methods, and a set of instance variables.

What is the difference between predicate and function in Java?

Function interface is used to do the transformation.It can accepts one argument and produces a result. On the other side, Predicate can also accept only one argument but it can only return boolean value. It is used to test the condition.


1 Answers

I'm assuming you're talking about com.google.common.base.Predicate<T> from Guava.

From the API:

Determines a true or false value for a given input. For example, a RegexPredicate might implement Predicate<String>, and return true for any string that matches its given regular expression.

This is essentially an OOP abstraction for a boolean test.

For example, you may have a helper method like this:

static boolean isEven(int num) {    return (num % 2) == 0; // simple } 

Now, given a List<Integer>, you can process only the even numbers like this:

    List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);     for (int number : numbers) {         if (isEven(number)) {             process(number);         }     } 

With Predicate, the if test is abstracted out as a type. This allows it to interoperate with the rest of the API, such as Iterables, which have many utility methods that takes Predicate.

Thus, you can now write something like this:

    Predicate<Integer> isEven = new Predicate<Integer>() {         @Override public boolean apply(Integer number) {             return (number % 2) == 0;         }                    };     Iterable<Integer> evenNumbers = Iterables.filter(numbers, isEven);      for (int number : evenNumbers) {         process(number);     } 

Note that now the for-each loop is much simpler without the if test. We've reached a higher level of abtraction by defining Iterable<Integer> evenNumbers, by filter-ing using a Predicate.

API links

  • Iterables.filter
    • Returns the elements that satisfy a predicate.

On higher-order function

Predicate allows Iterables.filter to serve as what is called a higher-order function. On its own, this offers many advantages. Take the List<Integer> numbers example above. Suppose we want to test if all numbers are positive. We can write something like this:

static boolean isAllPositive(Iterable<Integer> numbers) {     for (Integer number : numbers) {         if (number < 0) {             return false;         }     }     return true; }  //... if (isAllPositive(numbers)) {     System.out.println("Yep!"); } 

With a Predicate, and interoperating with the rest of the libraries, we can instead write this:

Predicate<Integer> isPositive = new Predicate<Integer>() {     @Override public boolean apply(Integer number) {         return number > 0;     }        };  //... if (Iterables.all(numbers, isPositive)) {     System.out.println("Yep!"); } 

Hopefully you can now see the value in higher abstractions for routines like "filter all elements by the given predicate", "check if all elements satisfy the given predicate", etc make for better code.

Unfortunately Java doesn't have first-class methods: you can't pass methods around to Iterables.filter and Iterables.all. You can, of course, pass around objects in Java. Thus, the Predicate type is defined, and you pass objects implementing this interface instead.

See also

  • Wikipedia/Higher-order function
  • Wikipedia/Filter (higher-order function)
like image 174
polygenelubricants Avatar answered Sep 25 '22 13:09

polygenelubricants