Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Predicates vs if statements

Tags:

I have seen in some projects that people use Predicates instead of pure if statements, as illustrated with a simple example below:

    int i = 5;     // Option 1     if (i == 5) {         // Do something         System.out.println("if statement");     }      // Option 2     Predicate<Integer> predicate = integer -> integer == 5;     if (predicate.test(i)) {         // Do something         System.out.println("predicate");     } 

What's the point of preferring Predicates over if statements?

like image 265
poyger Avatar asked Mar 16 '17 09:03

poyger


People also ask

What is a predicate method?

The predicate is a predefined functional interface in Java defined in the java. util. Function package. It helps with manageability of code, aids in unit-testing, and provides various handy functions.

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 the difference between a predicate integer and an IntPredicate?

Java IntPredicate interface is a predicate of one int-valued argument. It can be considered an operator or function that returns a value either true or false based on certain evaluation on the argument int value. IntPredicate is a functional interface whose functional method is boolean test(int a) .

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.


2 Answers

Using a predicate makes your code more flexible.

Instead of writing a condition that always checks if i == 5, you can write a condition that evaluates a Predicate, which allows you to pass different Predicates implementing different conditions.

For example, the Predicate can be passed as an argument to a method :

public void someMethod (Predicate<Integer> predicate) {     if(predicate.test(i)) {         // do something         System.out.println("predicate");     }     ... } 

This is how the filter method of Stream works.

like image 99
Eran Avatar answered Sep 24 '22 01:09

Eran


For the exact example that you provided, using a Predicate is a big over-kill. The compiler and then the runtime will create:

  1. a method (de-sugared predicate)
  2. a .class that will implement java.util.Predicate
  3. an instance of the class created at 2

all this versus a simple if statement.

And all this for a stateless Predicate. If your predicate is statefull, like:

 Predicate<Integer> p = (Integer j) -> this.isJGood(j); // you are capturing "this" 

then every time you will use this Predicate, a new instance will be created (at least under the current JVM).

The only viable option IMO to create such a Predicate is, of course, to re-use it in multiple places (like passing as arguments to methods).

like image 35
Eugene Avatar answered Sep 23 '22 01:09

Eugene