Junior
Do you use lambda expressions in your work?
sobes.tech AI
Answer from AI
Yes, I use them actively.
Lambda expressions significantly improve the readability and conciseness of code, especially when working with functional interfaces and streams.
Examples of usage:
-
In the Collections Framework:
// sorting a list of strings by length List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); names.sort((s1, s2) -> s1.length() - s2.length()); -
In the Stream API:
// filtering and mapping elements List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); List<Integer> evenSquares = numbers.stream() .filter(n -> n % 2 == 0) // filter .map(n -> n * n) // mapping .collect(Collectors.toList()); -
When working with multithreading (Runnable, Callable):
// creating a task to run in a separate thread Runnable task = () -> System.out.println("Task accomplished!"); new Thread(task).start(); -
When using functional interfaces:
// using a predefined functional interface Consumer<String> printer = s -> System.out.println("Printing: " + s); printer.accept("Hello");
Lambda expressions help create more flexible and expressive code, following the paradigm of functional programming. It is important to choose their use correctly to avoid reducing readability in case of overly complex expressions.