Java 8 DoublePredicate Interface

9 months ago codedestine 0

In this tutorial, we will learn about Java 8 DoublePredicate interface.

Java 8 DoublePredicate Interface

DoublePredicate Interface :-

DoublePredicate ( java.util.function.DoublePredicate ) is a functional interface that has one abstract method and three default method declared in it. This interface is a specialised form of Predicate interface.

Abstract method :- 

boolean  test ( double value ) :- The abstract method test ( double value ) is a functional method. It accepts one double value as an argument and return boolean value. It represents a conditional operation, where a passed double value is tested against a specific double value to give a boolean ( True or False ) result.

Default method :-  

  1. default  DoublePredicate<T>  and ( DoublePredicate <? super T > other ) :- It returns a doublepredicate that represents short circuiting logical AND of this doublepredicate ( doublepredicate on which method is called ) and another ( passed as an argument ).
  2. default DoublePredicate<T>  or ( DoublePredicate <? super T > other ) :- It returns a doublepredicate that represents short circuiting logical OR of this doublepredicate ( doublepredicate on which method is called ) and another ( passed as an argument ).
  3. default DoublePredicate<T>  negate (  ) :- It returns a doublepredicate that represents logical negation of this doublepredicate ( doublepredicate on which method is called ).

Reference :-  DoublePredicate Interface JavaDocs 

Example :- 

This example will show you, how to create and call different methods of a DoublePredicate interface.

  public static void main(String[] args){
 
    DoublePredicate doublePredicate1 = ( x ) -> { if( x == 56.66 ){ return true;
                                                  }else{ return false; }};
    // Example of test method
    boolean result = doublePredicate1.test(56.66);
    System.out.println(result);
 
    // Example of negate method
    boolean result3 = (doublePredicate1.negate()).test(56.66);
    System.out.println(result3);
 
    DoublePredicate doublePredicate2 = ( x ) -> { if( x == 96.78 ){ return true;
                                                  }else{ return false; }};
 
    // Example of and method
    boolean result1 = doublePredicate1.and(doublePredicate2).test(96.78);
    System.out.println(result1);
 
    // Example of or method
    boolean result2 = doublePredicate2.or(doublePredicate2).test(96.78);
    System.out.println(result2);
 
  }

Result :- 

true
false
false
true 

That’s all for Java 8 DoublePredicate Interface. If you liked it, please share your thoughts in comments section and share it with others too.