Java 8 Functional Interface With Example

Sanjay Singh
3 min readApr 3, 2024

Interface BiPredicate<T,U> vs Interface Predicate<T>
Function VS BiFunction
Consumer VS BiConsumer
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.
  • Syntax: @FunctionalInterface public interface BiPredicate<T, U> { boolean test(T t, U u); }
import java.util.function.BiPredicate;

public class BiPredicateExample {
public static void main(String[] args) {
BiPredicate<Integer, Integer> isGreaterThan = (a, b) -> a > b;

System.out.println(isGreaterThan.test(5, 3)); // Output: true
System.out.println(isGreaterThan.test(2, 8)); // Output: false
}
}

Function VS BiFunction

Function: Represents a function that accepts one argument and produces a result.

  • Example: Function<Integer, String> can be used to convert an integer to a string.
  • Syntax: @FunctionalInterface public interface Function<T, R> { R apply(T t); }
import java.util.function.Function;

public class FunctionExample {
public static void main(String[] args) {
Function<Integer, String> intToString = num -> "Number: " + num;

System.out.println(intToString.apply(5)); // Output: Number: 5
}
}

BiFunction: Represents a function that accepts two arguments and produces a result.

  • Example: BiFunction<Integer, Integer, Integer> can be used to calculate the sum of two integers.
  • Syntax: @FunctionalInterface public interface BiFunction<T, U, R> { R apply(T t, U u); }
import java.util.function.BiFunction;

public class BiFunctionExample {
public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;

System.out.println(add.apply(3, 5)); // Output: 8
}
}

Consumer VS BiConsumer

Consumer: Represents an operation that accepts a single input argument and returns no result.

  • Example: Consumer<String> can be used to print a string.
  • Syntax: @FunctionalInterface public interface Consumer<T> { void accept(T t); }
import java.util.function.Consumer;

public class ConsumerExample {
public static void main(String[] args) {
Consumer<String> printString = str -> System.out.println(str);

printString.accept("Hello, World!"); // Output: Hello, World!
}
}
  1. BiConsumer Represents an operation that accepts two input arguments and returns no result.
  • Example: BiConsumer<String, Integer> can be used to print a string along with its length.
  • Syntax: @FunctionalInterface public interface BiConsumer<T, U> { void accept(T t, U u); }
import java.util.function.BiConsumer;

public class BiConsumerExample {
public static void main(String[] args) {
BiConsumer<String, Integer> printStringLength = (str, length) -> System.out.println(str + " has length " + length);

printStringLength.accept("Java", 4); // Output: Java has length 4
}
}

Supplier -

Supplier is a functional interface that represents a supplier of results. It does not accept any arguments but produces a result of a given generic type. It is typically used when you need to generate or supply values without taking any input

@FunctionalInterface
public interface Supplier<T> {
T get();
}
import java.util.function.Supplier;

public class SupplierExample {
public static void main(String[] args) {
Supplier<String> supplier = () -> "Hello, World!";

String result = supplier.get();
System.out.println(result); // Output: Hello, World!
}
}

--

--

Sanjay Singh

Java||Spring-Boot|| Micro services Application developer|| INDIA