Type to search.

Lambdas

Serverless

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

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:

Java
// 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.