Junior — Middle
Have you used functional interfaces in your projects and how?
sobes.tech AI
Answer from AI
In Java, functional interfaces are used to simplify code and improve readability, especially with lambda expressions introduced in Java 8.
For example, the Runnable interface — a functional interface with a single method run(). It is convenient for creating threads:
Runnable task = () -> System.out.println("Task execution in a separate thread");
new Thread(task).start();
Also, I often used interfaces from java.util.function, such as Function<T, R>, Predicate<T>, Consumer<T> for collection processing and data stream handling:
List<String> names = Arrays.asList("Anna", "Bob", "Charlie");
// Using Predicate for filtering
List<String> filtered = names.stream()
.filter(name -> name.startsWith("A"))
.collect(Collectors.toList());
Functional interfaces allow writing compact and expressive code, especially when working with Stream API and event handling.