Sobes.tech
Junior — Middle

Kas saaksite selgitada lambda-väljendite tööpõhimõtet programmeerimises?

sobes.tech AI

Vastus AI-lt

Lambda expressions are anonymous functions that can be passed as objects and used for concise functional code writing. In Java, they allow writing compact code to implement interfaces with a single method (functional interfaces).

Principle of operation:

  • A lambda expression describes behavior (a function) without the need to create a separate class.
  • It can accept parameters and return results.
  • It is used, for example, for processing collections, events, or streams.

Example:

List<String> names = Arrays.asList("Anna", "Bob", "Charlie");
names.forEach(name -> System.out.println(name));

Here, name -> System.out.println(name) is a lambda expression that prints each element of the list to the screen.