Unfortunately, there are no lambdas in Java until Java 8 introduced Lambda Expressions. However, you can get almost the same effect (in a really ugly way) with anonymous classes:

interface MyLambda {
    void theFunc(); // here we define the interface for the function
}

public class Something {
    static void execute(MyLambda l) {
        l.theFunc(); // this class just wants to use the lambda for something
    }
}

public class Test {
    static void main(String[] args) {
        Something.execute(new MyLambda() { // here we create an anonymous class
            void theFunc() {               // implementing MyLambda
                System.out.println("Hello world!");
            }
        });
    }
}

Obviously these would have to be in separate files :(

Answer from Zifre on Stack Overflow
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › lambda runtimes
Lambda runtimes - AWS Lambda
Lambda is agnostic to your choice of runtime. For simple functions, interpreted languages like Python and Node.js offer the fastest performance. For functions with more complex computation, compiled languages like Java are often slower to initialize but run quickly in the Lambda handler.
Discussions

if statement - Use Java lambda instead of 'if else' - Stack Overflow
In Java 8 we can use lambda forEach instead of a traditional for loop. In programming for and if are two basic flow controls. If we can use lambda for a for loop, why is using lambda for if bad idea? for (Element element : list) { element.doSomething(); } list.forEach(Element::doSomething); More on stackoverflow.com
🌐 stackoverflow.com
Why has Java fallen out of favor for use in lambdas?
Were they ever IN favor? Cold start times for JVM based lambdas were awful when I experimented with them a few years ago. I've read recently that improvements have been made, but honestly I like my lambdas to be extremely light weight, so interpreted languages just feel more in line with what I'm doing. More on reddit.com
🌐 r/aws
52
8
November 28, 2022
can I use an if statement with a lambda function?
It helps to debug if you split your 1 liner into multiple variable steps at each function. I haven't worked with python but it appears the problem may be in your last map. It appears what you want is to filter for len(x['pdd_list']) == 0 in a filter function. .map(lambda x: x['pdd_list']) . filter(lambda x: len(x['pdd_list'])==0) More on reddit.com
🌐 r/apachespark
4
4
August 12, 2019
In what scenario does using Java in Lambda make sense?
Very often the best language for a team/org is not the "best" language for particular component. If it's a Java based team it often doesn't make sense to dump all that investment just for Lambda if Java performance there is acceptable. Personally I won't use Node for anything if I can possibly avoid it. If Python isn't fast enough and in particular if threading will help I'll use Go. But even as anti-Node as I admit I am, I absolutely respect that in shops with a lot of Javascript talent due to frontend work it often makes the most sense to go with Node for backend work despite its many hair pulling issues. It's much better to be pragmatic than "right". Lambda supports a ton of languages (effectively all of them if we count custom runtimes) because it's pragmatic. More on reddit.com
🌐 r/aws
62
25
March 28, 2024
🌐
Coderanch
coderanch.com › t › 777533 › languages › Lambda-fuctions-Python-compared-Java
Lambda fuctions in Python as compared to Java Lambda function (Jython/Python forum at Coderanch)
November 7, 2023 - The main purpose of lambda expressions in Java is the same as the purpose of lambda expressions in Python, and indeed, in ANY functional language: To be able to pass anonymous functions to a higher order function. . The main purpose is to to be able to pass annomous functions to a higher order functions. Earlier, before Java 8 since lambda was not there, to higher order function, function of anonymous class was passed.
🌐
W3Schools
w3schools.com › java › java_lambda.asp
Java Lambda Expressions
Lambda Expressions were added in Java 8. A lambda expression is a short block of code that takes in parameters and returns a value. Lambdas look similar to methods, but they do not need a name, and they can be written right inside a method body.
🌐
DEV Community
dev.to › myexamcloud › understanding-and-using-lambda-functions-in-python-and-java-3l58
Understanding and Using Lambda Functions in Python and Java - DEV Community
April 30, 2024 - Lambda functions are advantageous for simple expressions that don't require a full function, and they can also be used within regular functions. By using return statements, you can incorporate Lambda functions into your regular functions for a more efficient option. In Java, Lambda expressions were introduced in Java 8.
Top answer
1 of 5
39

As it almost but not really matches Optional, maybe you might reconsider the logic:

Java 8 has a limited expressiveness:

Optional<Elem> element = ...
element.ifPresent(el -> System.out.println("Present " + el);
System.out.println(element.orElse(DEFAULT_ELEM));

Here the map might restrict the view on the element:

element.map(el -> el.mySpecialView()).ifPresent(System.out::println);

Java 9:

element.ifPresentOrElse(el -> System.out.println("Present " + el,
                        () -> System.out.println("Not present"));

In general the two branches are asymmetric.

2 of 5
21

It's called a 'fluent interface'. Simply change the return type and return this; to allow you to chain the methods:

public MyClass ifExist(Consumer<Element> consumer) {
    if (exist()) {
        consumer.accept(this);
    }
    return this;
}

public MyClass ifNotExist(Consumer<Element> consumer) {
    if (!exist()) {
        consumer.accept(this);
    }
    return this;
}

You could get a bit fancier and return an intermediate type:

interface Else<T>
{
    public void otherwise(Consumer<T> consumer); // 'else' is a keyword
}

class DefaultElse<T> implements Else<T>
{
    private final T item;

    DefaultElse(final T item) { this.item = item; }

    public void otherwise(Consumer<T> consumer)
    {
        consumer.accept(item);
    }
}

class NoopElse<T> implements Else<T>
{
    public void otherwise(Consumer<T> consumer) { }
}

public Else<MyClass> ifExist(Consumer<Element> consumer) {
    if (exist()) {
        consumer.accept(this);
        return new NoopElse<>();
    }
    return new DefaultElse<>(this);
}

Sample usage:

element.ifExist(el -> {
    //do something
})
.otherwise(el -> {
    //do something else
});
🌐
W3Schools
w3schools.com › python › python_lambda.asp
Python Lambda
Python If Python Elif Python Else Shorthand If Logical Operators Nested If Pass Statement Code Challenge Python Match ... Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range
Find elsewhere
🌐
Steven Skelton
stevenskelton.ca › home
JVM versus Python for AWS Lambda Functions - Steven Skelton
August 13, 2022 - Benchmarks show Python offers the best performances, and the language simplicity normally results in faster development and less code for lightweight tasks. But AWS Lambda can offer massive scale with access to up to 10GB RAM and 15 minutes per execution that are typically benefited from the structure and maintainability offered in languages such as Java, Scala, and C#. This article investigates how the JVM stacks up on the low-end and if languages like Python are the only choice.
🌐
Medium
medium.com › @afinlay › lambda-expressions-in-java-python-c-c-8cdbca5a5e8b
Lambda Expressions in Java, Python, C#, C++ | by Adrian D. Finlay | Medium
May 1, 2018 - + "Restarting.\n\n"); main(args); } //Method Reference (Lambda Expression) doMath = Math::min; //Print Result out.println("The result is:\t" + doMath.binaryMathOp(expr1, expr2) + "\n"); //Close Input Stream in.close(); } } ... Lambda Expressions work a little differently in python. In python, Lambda Expressions are essentially anonymous functions. Also, note that unlike Java, we can define it’s behavior on the fly in the same way.
🌐
Joshdata
joshdata.me › lambda-expressions.html
Lambda Expressions: A Guide
In C++, C#, Java, Javascript, and Python, any regular function name or class method can also be assigned to a variable and passed to a function, like lambda expressions. In the statically typed languages, the variable or function argument must have the right type. But in dynamically typed languages, that’s not an issue and passing around functions can be very natural: ... def min(x, y): if x < y: return x return y def max(x, y): if x > y: return x return y foo(min, 10, 20); # prints 10 foo(max, 10, 20); # prints 20 def foo(f, x, y) { f(x, y); }
🌐
Scanner
scanner.dev › blog › serverless-speed-rust-vs-go-java-and-python-in-aws-lambda-functions
Serverless Speed: Rust vs. Go, Java, and Python in AWS Lambda Functions
September 11, 2025 - In particular, SnapStart does not support: ... In general, we were disappointed with Java’s performance in Lambda functions for this bursty data-processing use case, so we recommend trying Rust or Go instead if you can. Of the four languages, Python’s Lambda function code is the simplest.
🌐
Tutorialspoint
tutorialspoint.com › java › java-lambda-expressions.htm
Java - Lambda Expressions
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class JavaTester { public static void main(String args[]) { // prepare a list of strings List<String> list = new ArrayList<>(); list.add("java"); list.add("html"); list.add("python"); // print the list using a lambda expression // here we're passing a lambda expression to forEach // method of list object list.forEach(i -> System.out.println(i)); List<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); numbers.add(6); numbers.add(7); numbers.add
🌐
Quora
quora.com › How-do-lambda-functions-in-Python-compare-to-those-in-other-programming-languages-like-JavaScript-or-Java
How do lambda functions in Python compare to those in other programming languages like JavaScript or Java? - Quora
Answer: They are damn complicated and incoherent. I think the most straightforward way to learn them is to write some simple lambdas in Java, look at the generated bytecode and then compare how other languages unnecessarily complicate them (or not restrict them “for convenience of newbies”, but ...
🌐
Medium
medium.com › @gamini782 › python-vs-java-in-aws-lambda-a-deep-dive-into-bulk-sql-insertions-cdf0640a11f3
Python vs Java in AWS Lambda: A Deep Dive into Bulk SQL Insertions | by Gamini Sharma | Medium
July 24, 2025 - This post unpacks the nuances of Python’s executemany() versus Java’s JDBC batch inserts and explains which is better. Python’s executemany() method allows you to submit multiple SQL insert statements in one go. It’s concise, efficient, and a favorite for quick data operations in serverless setups. ... import psycopg2 from psycopg2 import extrasdef lambda_handler(event, context): conn = psycopg2.connect(...) cursor = conn.cursor() data = [("Java", 1995), ("Python", 1991)] extras.execute_values(cursor, "INSERT INTO langtable(name, year) VALUES %s", data) conn.commit() cursor.close() conn.close()return {"statusCode": 200, "body": "Success"}
🌐
Rice
clear.rice.edu › comp310 › JavaResources › lambdas.html
Lambda Functions
Those who know Python should recognize this extra parameter as the "self" parameter that must always be the first parameter of any method declaration. Under the hood, Java also has this extra input parameter but does not force the programmer to explicitly declare it. Thus, N+1 input parameters declared by the associated functional interface is really just the real underlying method signature of the desired method. ... In this scenario, the desired lambda is a static method of a class.
🌐
Learn IT University
learn-it-university.com › home › effortless element existence check: using lambda expressions in python
Effortless Element Existence Check: Using Lambda Expressions in Python - Learn IT University
July 21, 2024 - Checking if an element exists within a collection in Java is a common task that developers face regularly. Using lambda expressions provides a clean and efficient way to accomplish this. One of the most effective methods for checking existence is using the anyMatch method introduced in Java ...
🌐
Reddit
reddit.com › r/aws › in what scenario does using java in lambda make sense?
In what scenario does using Java in Lambda make sense? : r/aws
March 28, 2024 - If it's a Java based team it often doesn't make sense to dump all that investment just for Lambda if Java performance there is acceptable. Personally I won't use Node for anything if I can possibly avoid it. If Python isn't fast enough and in particular if threading will help I'll use Go.