Sobes.tech
Junior

What are lambda expressions in Java?

sobes.tech AI

Answer from AI

Lambda expressions in Java are a concise way to describe anonymous functions that can be passed as arguments or used to implement functional interfaces. They allow writing more succinct and readable code, especially when working with collections and streams.

Example of a lambda expression that sorts a list of strings by length:

List<String> names = Arrays.asList("Anna", "John", "Elizabeth");
names.sort((a, b) -> a.length() - b.length());

Here, (a, b) -> a.length() - b.length() is a lambda expression that implements the comparison method for sorting.