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— methodvoid run(), used to execute code in a separate thread.Callable<V>— methodV call(), returns a result and can throw exceptions.Supplier<T>— methodT get(), supplies objects without input parameters.Consumer<T>— methodvoid accept(T t), accepts an object and returns nothing.Function<T,R>— methodR apply(T t), transforms an object of type T into an object of type R.Predicate<T>— methodboolean 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