Tuesday, August 13, 2019

Java 8 IntPredicate Examples

1. Overview

In this tutorial, We'll be learning the new java 8 IntPredicate functional Interface with examples. This is part of java.util.function package.

In the last article, We've seen an in-depth article on Predicate Function Interface in Java 8. This works exactly similar to the Predicate. Predicate works for all data types but IntPredicate works only for int values.


Short Introduction:

IntPredicate represents a predicate (boolean-valued function) of one int-valued argument. This is the int-consuming primitive type specialization of Predicate. IntPredicate interface has a functional method test() which takes int as an argument and returns a boolean value. This interface has only one abstract method hence it is called FI.

We will be seeing the syntax and its example programs on test(int value), and(IntPredicate other), negate() and or(IntPredicate other) methods.

test() is an abstract method and the remaining 3 are default methods.



2. test(int value) method example


This is a functional method of IntPredicate interface which is also declared as abstract.

Syntax:

boolean test(int value)

This method evaluates the condition on the given value and returns a boolean value.

The condition in java 8 is called Predicate.

Example:

See the below example to find the person is major or not based on the age provided. The person is major if the age is greater than 18.

package com.java.w3schools.blog.java8.functional.intpredicate;

import java.util.function.IntPredicate;

public class IntPredicateExample {

 public static void main(String[] args) {

  IntPredicate agePredicate = age -> age > 18;

  boolean isMajor = agePredicate.test(20);
  System.out.println("Age 20 is major : " + isMajor);

  isMajor = agePredicate.test(10);
  System.out.println("Age 10 is major : " + isMajor);
 }

}

Output:

Age 20 is major : true
Age 10 is major : false

3. and(IntPredicate other) example


and() method is to combine the current predicate with another predicate. If both are true then it returns true else false. This is a default method.

Syntax:

default IntPredicate and(IntPredicate other)

This takes IntPredicate as an argument and returns new IntPredicate. The returned new predicate checks two conditions when we call test() method.

newAndredicate = predicate1.and(predicate2);

If predicate1 is false then it will not check the predicate2.

Example:

package com.java.w3schools.blog.java8.functional.intpredicate;

import java.util.function.IntPredicate;

public class IntPredicateAndExample {

 public static void main(String[] args) {

  IntPredicate predicate1 = amount -> amount > 0;
  IntPredicate predicate2 = amount -> amount % 500 == 0;

  IntPredicate andPredicate = predicate1.and(predicate2);

  boolean isValidAmt = andPredicate.test(1000);
  System.out.println("Rs. 1000 is valid ? " + isValidAmt);

  isValidAmt = andPredicate.test(1300);
  System.out.println("Rs. 1300 is valid ? " + isValidAmt);

  isValidAmt = andPredicate.test(-5000);
  System.out.println("Rs. -5000 is valid ? " + isValidAmt);

 }

}

Output:

Rs. 1000 is valid ? true
Rs. 1300 is valid ? false
Rs. -5000 is valid ? false

4. or(IntPredicate other) Example


or() method also takes IntPredicate as an argument and if any one of the predicate satisfies then it returns true else false.

newOrPredicate = predicate1.or(predicate2);

If the first predicate1 is true then it will not check the condition in predicate2. If and only if the first predicate fails then checks predicate2.

Syntax:

default IntPredicate or(IntPredicate other)

This is also a default method.

Example:

package com.java.w3schools.blog.java8.functional.intpredicate;

import java.util.function.IntPredicate;

public class IntPredicateAndExample {

 public static void main(String[] args) {

  IntPredicate predicate1 = amount -> amount > 0;
  IntPredicate predicate2 = amount -> amount % 500 == 0;

  IntPredicate orPredicate = predicate1.or(predicate2);

  boolean isValidAmt = orPredicate.test(1000);
  System.out.println("Rs. 1000 is valid ? " + isValidAmt);

  isValidAmt = orPredicate.test(-1300);
  System.out.println("Rs. 1300 is valid ? " + isValidAmt);

  isValidAmt = orPredicate.test(-5000);
  System.out.println("Rs. -5000 is valid ? " + isValidAmt);

 }

}

Output:

Rs. 1000 is valid ? true
Rs. 1300 is valid ? false
Rs. -5000 is valid ? true

5. negate() example


This works similar to the logical negate function. If the predicate is true then it returns false and vice-versa.

Syntax:

default IntPredicate negate()

This is also a default function and returns a new predicate by reversing the condition.

Example:

Checking the minor example program using negate() method.

package com.java.w3schools.blog.java8.functional.intpredicate;

import java.util.function.IntPredicate;

public class IntPredicatenegateExample {

 public static void main(String[] args) {

  IntPredicate agePredicate = age -> age > 18;
  IntPredicate negatePredicate = agePredicate.negate();

  boolean isMajor = negatePredicate.test(20);
  System.out.println("Age 20 is minor : " + isMajor);

  isMajor = negatePredicate.test(10);
  System.out.println("Age 10 is minor : " + isMajor);
 }

}

Output:

Age 20 is minor : false
Age 10 is minor : true

6. Internal Implementation


 default IntPredicate negate() {
     return (value) -> !test(value);
 }
 
 default IntPredicate and(IntPredicate other) {
      Objects.requireNonNull(other);
      return (value) -> test(value) && other.test(value);
  }
 
 
 default IntPredicate or(IntPredicate other) {
     Objects.requireNonNull(other);
     return (value) -> test(value) || other.test(value);
 }

6. Conclusion

In this article, We've covered an int specific Predicate functional interface with example programs.

Shown example programs for each method of the interface.

Shown how it works internally for default methods.

GitHub
API Ref
Predicate Examples

No comments:

Post a Comment