Sobes.tech
Junior — Middle

Can you give examples of functional interfaces in programming?

sobes.tech AI

Answer from AI

Functional interfaces are interfaces with a single abstract method, widely used in Java for working with lambda expressions and functional programming.

Examples of standard functional interfaces in Java:

  • Runnable — method void run(), used to execute code in a separate thread.
  • Callable<V> — method V call(), returns a result and can throw exceptions.
  • Supplier<T> — method T get(), supplies objects without input parameters.
  • Consumer<T> — method void accept(T t), accepts an object and returns nothing.
  • Function<T,R> — method R apply(T t), transforms an object of type T into an object of type R.
  • Predicate<T> — method boolean test(T t), checks a condition and returns a boolean value.

Example of using Function:

Function<String, Integer> stringLength = s -> s.length();
int len = stringLength.apply("Hello"); // len = 5
Can you give examples of functional interfaces in… - sobes.tech