elasticsearch
Loading

Lambdas

Serverless Stack

Lambda expressions are anonymous functions that provide a concise way to write short, inline functions without declaring them explicitly. Lambdas are particularly useful for functional programming operations such as filtering, mapping, and sorting collections.

Lambda expressions and method references work the same as in Java, providing familiar syntax for developers. They allow you to write more compact and expressive code when working with collections and functional interfaces.

Lambdas use the arrow syntax (->) to separate parameters from the function body. You can use lambdas with or without explicit type declarations.

Basic lambda expressions:

// Removes all elements equal to 2
list.removeIf(item -> item == 2);
list.removeIf((int item) -> item == 2);
list.removeIf((int item) -> { item == 2 });

// Sorts list in ascending order
list.sort((x, y) -> x - y);
// Sorts list in ascending order using method reference
list.sort(Integer::compare);
		

You can make method references to functions within the script with this, for example list.sort(this::mycompare). Method references provide a shorthand notation for lambdas that call a specific method.

Lambdas are commonly used for:

  • Filtering collections: Remove elements that meet specific criteria.
  • Sorting data: Define custom comparison logic.
  • Transforming values: Apply operations to collection elements.
  • Functional operations: Work with streams and functional interfaces.