Sobes.tech
Junior — Middle

Have you had experience working with functional interfaces in your projects?

sobes.tech AI

Answer from AI

Yes, in Java projects I have used functional interfaces, especially after the introduction of lambda expressions in Java 8. Functional interfaces are interfaces with a single abstract method, which allow the use of lambdas for more concise and expressive code.

Examples of standard functional interfaces:

  • Runnable — with no parameters and no return value.
  • Supplier<T> — supplies an object of type T.
  • Consumer<T> — accepts an object of type T, returns nothing.
  • Function<T,R> — accepts T and returns R.
  • Predicate<T> — accepts T and returns a boolean.

Example of using Function with a lambda:

import java.util.function.Function;

Function<String, Integer> stringLength = s -> s.length();
int len = stringLength.apply("Hello"); // len = 5

I also created my own functional interfaces for specific tasks, using the @FunctionalInterface annotation to explicitly indicate that the interface is intended for lambdas.

Have you had experience working with functional… - sobes.tech