Java 8 – Primitive Predicate Functional Interface

In this article, we will discuss different variation of pre-defined Predicate available for primitive data-types like int, long, double, etc. in Java 1.8 version

Predicate primitive Functional Interface :

  • This is very similar to Predicate but it always accepts 1 input argument of type primitive-type like int, long and double, whereas Predicate allows to accept any data-type and return result in boolean value either true/false
  • Performance-wise primitive Predicate is much faster compared to Predicate<T>
  • There are lot of conversion happening for auto-boxing & auto-unboxing for converting primitive-type to wrapper-type and again wrapper-type to primitive-type and so on
  • To avoid unnecessary conversion between primitive-type to wrapper-type and vice-versa, primitive-type specific Predicate Functional Interface for conversion introduced in Java 1.8 version as listed below,
  1. IntPredicate
  2. LongPredicate
  3. DoublePredicate

We will look into method signature along with example for each one of the above primitive Predicate Functional Interface

1. IntPredicate Functional Interface

  • This primitive IntPredicate accepts 1-input argument of primitive-type int and it is not required to declare while defining IntPredicate (or lambda expression)
  • Method signature : Below is the IntPredicate functional interface with single abstract test(); method along with other default and static methods
package java.util.function;

import java.util.Objects;

@FunctionalInterface
public interface IntPredicate {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param value the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(int value);
    
    // other default and static methods
}

1.1 Example for IntPredicate :

  • Here, while defining lambda expression using IntPredicate we haven’t specified any data-type like Integer still compiler doesn’t complain and executed well
  • Caution: but if we pass any other data-type like String or double then compile-time error will be thrown stating “The method test(int) in the type IntPredicate is not applicable for the arguments (double)
package net.bench.resources.primitive.predicate.example;

import java.util.function.IntPredicate;

public class IntPredicateExample {

	public static void main(String[] args) {

		// lambda expression for IntPredicate to check number is even/odd
		IntPredicate ip = i -> i%2 == 0;

		// testing for different numbers using above lambda expression
		System.out.println("1. Whether 10 is even number : " + ip.test(10));

		System.out.println("2. Whether 7 is even number : " + ip.test(7));

		System.out.println("3. Whether 19 is even number : " + ip.test(19));

		System.out.println("4. Whether 32 is even number : " + ip.test(32));

		System.out.println("5. Whether 41 is even number : " + ip.test(41));
	}
}

Output:

1. Whether 10 is even number : true
2. Whether 7 is even number : false
3. Whether 19 is even number : false
4. Whether 32 is even number : true
5. Whether 41 is even number : false

2. LongPredicate Functional Interface

  • This primitive LongPredicate accepts 1-input argument of primitive-type long and it is not required to declare while defining LongPredicate (or lambda expression)
  • Method signature : Below is the LongPredicate functional interface with single abstract test(); method along with other default and static methods
package java.util.function;

import java.util.Objects;

@FunctionalInterface
public interface LongPredicate {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param value the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(long value);
    
    // other default and static methods
}

2.1 Example for LongPredicate :

  • Here, while defining lambda expression using LongPredicate we haven’t specified any data-type like Long still compiler doesn’t complain and executed well
  • Caution: but if we pass any other data-type like String or double then compile-time error will be thrown stating “The method test(long) in the type LongPredicate is not applicable for the arguments (double)
package net.bench.resources.primitive.predicate.example;

import java.util.function.LongPredicate;

public class LongPredicateExample {

	public static void main(String[] args) {

		// lambda expression for LongPredicate to check number is positive or negative
		LongPredicate lp = longNum ->  longNum >= 0;

		// testing for different numbers using above lambda expression
		System.out.println("1. Whether 10 is postive number : " + lp.test(10));
		System.out.println("2. Whether -7 is postive number : " + lp.test(-7));
		System.out.println("3. Whether 19 is postive number : " + lp.test(19));
		System.out.println("4. Whether -32 is postive number : " + lp.test(-32));
		System.out.println("5. Whether 41 is postive number : " + lp.test(41));
	}
}

Output:

1. Whether 10 is postive number : true
2. Whether -7 is postive number : false
3. Whether 19 is postive number : true
4. Whether -32 is postive number : false
5. Whether 41 is postive number : true

3. DoublePredicate Functional Interface

  • This primitive DoublePredicate accepts 1-input argument of primitive-type double and it is not required to declare while defining DoublePredicate (or lambda expression)
  • Method signature : Below is the DoublePredicate functional interface with single abstract test(); method along with other default and static methods
package java.util.function;

import java.util.Objects;

@FunctionalInterface
public interface DoublePredicate {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param value the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(double value);
    
    // other default and static methods
}

3.1 Example for DoublePredicate :

  • Here, while defining lambda expression using DoublePredicate we haven’t specified any data-type like Double still compiler doesn’t complain and executed well
  • Caution: but if we pass any other data-type like String then compile-time error will be thrown stating “The method test(double) in the type DoublePredicate is not applicable for the arguments (String)
package net.bench.resources.primitive.predicate.example;

import java.util.function.DoublePredicate;

public class DoublePredicateExample {

	public static void main(String[] args) {

		// lambda expression for DoublePredicate to check CGPA is greater than 6.7
		DoublePredicate dp = d ->  d >= 6.7;

		// testing for different numbers using above lambda expression
		System.out.println("1. Whether Suresh passed with CGPA of 9.2 : " 
				+ dp.test(9.2));
		System.out.println("2. Whether Naresh passed with CGPA of 4.3 : " 
				+ dp.test(4.3));
		System.out.println("3. Whether Rajesh passed with CGPA of 7.9 : " 
				+ dp.test(7.9));
		System.out.println("4. Whether Ramesh passed with CGPA of 5.4 : " 
				+ dp.test(5.4));
		System.out.println("5. Whether Lokesh passed with CGPA of 6.8 : " 
				+ dp.test(6.8));
	}
}

Output:

1. Whether Suresh passed with CGPA of 9.2 : true
2. Whether Naresh passed with CGPA of 4.3 : false
3. Whether Rajesh passed with CGPA of 7.9 : true
4. Whether Ramesh passed with CGPA of 5.4 : false
5. Whether Lokesh passed with CGPA of 6.8 : true

References:

Happy Coding !!
Happy Learning !!

Java 8 - Primitive Function Functional Interface
Java 8 - BiConsumer Functional Interface