Junior — Middle
Do you use anonymous functions or lambda expressions in your code?
sobes.tech AI
Answer from AI
Yes, anonymous functions and lambda expressions are widely used in Java, especially starting from Java 8.
They allow writing compact and readable code, particularly when working with functional interfaces, such as in Streams or event handlers.
Example of using a lambda expression to sort a list of strings by length:
List<String> names = Arrays.asList("Anna", "John", "Elizabeth");
names.sort((a, b) -> a.length() - b.length());
Anonymous functions simplify the code, reduce boilerplate, and make it more expressive.