Member-only story
Java 8 Functional Interface With Example
3 min readApr 3, 2024
1-Interface BiPredicate<T,U> vs Interface Predicate<T>
2-Function VS BiFunction
3-Consumer VS BiConsumer
4-Supplier
BiPredicate<T,U> vs Predicate<T>
Predicate<T>
:
- This interface represents a predicate, which is a boolean-valued function of one argument. It’s commonly used for filtering collections or testing conditions.
- Example:
Predicate<String>
can be used to test if a string starts with "A". - Syntax:
@FunctionalInterface public interface Predicate<T> { boolean test(T t);
import java.util.function.Predicate;
public class PredicateExample {
public static void main(String[] args) {
Predicate<String> startsWithA = str -> str.startsWith("A");
System.out.println(startsWithA.test("Apple")); // Output: true
System.out.println(startsWithA.test("Banana")); // Output: false
}
}
BiPredicate<T, U>
:
- This interface is similar to
Predicate
, but it takes two arguments. It represents a predicate which accepts two arguments and returns a boolean result. - Example:
BiPredicate<Integer, Integer>
can be used to test if one integer is greater than another.