October 2, 2022

BiPredicate Functional Interface Java Examples

In this post we’ll see examples of java.util.function.BiPredicate functional interface.

BiPredicate functional interface represents a boolean valued function that takes two arguments and returns either true or false. Abstract method in this functional interface is-

test(T t, U u)- This method evaluates the predicate on the passed arguments and returns either true or false based on whether the input arguments match the predicate or not.

If you are writing a Lambda expression that takes two arguments and uses those arguments to evaluate a condition that returns true or false then that lambda expression can be written as an implementation of BiPredicate built-in functional interface.

Apart from the test(T t, U u) abstract method Predicate interface has following default interface methods.

  • and(BiPredicate<? super T,? super U> other)- This default method returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
  • or(BiPredicate<? super T,? super U> other)- This default method returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
  • negate()- This default method returns a predicate that represents the logical negation (reverse condition) of the passed predicate.

BiPredicate interface test() method example

import java.util.function.BiPredicate;

public class BiPredicateExample {
  public static void main(String[] args) {
    BiPredicate<String, String> bp = (s1, s2) -> s1.equals(s2);  
    boolean val = bp.test("knpcode.com", "knpcode.com");
    System.out.println("val is- " + val);
    val = bp.test("Hello", "Test");
    System.out.println("val is- " + val);
  }
}
Output
val is- true
val is- false

In the example this lamdba expression- BiPredicate<String, String> bp = (s1, s2) -> s1.equals(s2); is the implementation of BiPredicate interface. When test() method with two arguments is called Java can infer from the context that lambda expression is the implementation of test() method.

2. In the second example we’ll write a method that takes BiPredicate as a method argument. Method checks whether the file at the given path is a regular file or a directory.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.BiPredicate;

public class BiPredicateExample {
  public static void main(String[] args) throws IOException {
    // Called with directory - returns false
    Path path = Paths.get("F:\\knpcode\\Parent");
    System.out.println(isFile(path, (p, fileAttributes) -> fileAttributes.isRegularFile()));
    // Called with File - returns false
    path = Paths.get("F:\\knpcode\\Parent\\Test.txt");
    System.out.println(isFile(path, (p, fileAttributes) -> fileAttributes.isRegularFile()));
  }
	
  private static boolean isFile(Path path, BiPredicate<Path, BasicFileAttributes> matcher) throws IOException {
    return matcher.test(path, Files.readAttributes(path, BasicFileAttributes.class));
  }
}
Output
false
true

BiPredicate functional interface in JDK

These built-in functional interfaces are used extensively by the Java language itself. You can fine the usage of BiPredicate functional interface in Files.find() method.

Stream<Path> find(Path start, int maxDepth, BiPredicate<Path,BasicFileAttributes> matcher, FileVisitOption... options)- This method returns a Stream that is lazily populated with Path by searching for files in the given path. Files that will be included in the stream is determined by the passed BiPredicate.

Here is an example that lists all the files in the passed directory and its child directories upto the depth of 10.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class BiPredicateExample {
  public static void main(String[] args) throws IOException {
    Path startPath = Paths.get("F:\\knpcode\\Parent");
    Stream<Path> fileStream = Files.find(startPath, 10, (path, fileAttributes) -> fileAttributes.isRegularFile());
    fileStream.forEach(System.out::println);
    fileStream.close();
  }
}

That's all for the topic BiPredicate Functional Interface Java Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment