Lambdas are typically used when you're passing a callback function as a parameter to another object or method. Technically, functions in Java are not objects, so a "lambda function" actually gives you an object that implements an interface. There are other ways to do this, but lambdas are very concise and keep the code of the callback "inline" at the point where you're using it. Consider, for instance, the Swing JButton class. To make the button actually do something when clicked, you call addActionListener which expects as its parameter an implementation of the ActionListener interface. Say you want to print the string "Hello world!" when the button is clicked. You could write a whole separate HelloWorldActionListener class that implements ActionListener. Or you could write it as an inline anonymous class: button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Hello world!"); } }); A lambda function lets you do the same thing much more concisely: button.addActionListener(e -> System.out.println("Hello world!")); Answer from teraflop on reddit.com
🌐
Reddit
reddit.com › r/learnprogramming › can someone explain lambdas?
r/learnprogramming on Reddit: Can someone explain lambdas?
November 14, 2023 -

So I’m reading a book on Java, and it’s talking about lambdas. The syntax seems simple enough, but the author hasn’t described why you would use them over a regular function. They’re being used in the context of functions that don’t have bodies (abstracts, I think?), but I don’t understand why I would use those either.

Top answer
1 of 16
61
Lambdas are typically used when you're passing a callback function as a parameter to another object or method. Technically, functions in Java are not objects, so a "lambda function" actually gives you an object that implements an interface. There are other ways to do this, but lambdas are very concise and keep the code of the callback "inline" at the point where you're using it. Consider, for instance, the Swing JButton class. To make the button actually do something when clicked, you call addActionListener which expects as its parameter an implementation of the ActionListener interface. Say you want to print the string "Hello world!" when the button is clicked. You could write a whole separate HelloWorldActionListener class that implements ActionListener. Or you could write it as an inline anonymous class: button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Hello world!"); } }); A lambda function lets you do the same thing much more concisely: button.addActionListener(e -> System.out.println("Hello world!"));
2 of 16
18
Lambdas are basically anonymous, ad-hoc functions which are treated as first-class variables and can be passed around like any other object. They allow you to inject behavior as a dependency/parameter. They are very useful for cutting down on boilerplate. For example, let's say you have the following boilerplate in C#: StartTimer(); result = DoSomething(); StopTimer(); LogResultAndTime(result); StartTimer(); result = DoSomethingElse(); StopTimer(); LogResultAndTime(result); //ad nauseum Creating a regular method like so: void BoilerplateCode(int result) { StartTimer(); //What do you put here? StopTimer(); LogResultAndTime(result); //result was already calculated before it was passed in, so the result was not properly timed! } Will not work. However, if you use a lambda, you can make it work: void Boilerplate(Func lambda) { StartTimer(); var result = lambda(); //Invoke the lambda on behalf of the caller so that we can time the function that was passed in. StopTimer(); LogResultAndTime(result); } //Usage example: Boilerplate(() => DoSomething()/*This does not get executed until the lambda is invoked*/); Boilerplate(() => DoSomethingElse()); Boilerplate(() => DoSomethingOther()); // ad nauseum As you can see, using a lambda allows me to only define my boilerplate code once, instead of needing to repeat it each time I want to use it. This allows me to easily make an enhancement, such as adding exception handling: void Boilerplate(Func lambda) { try { StartTimer(); var result = lambda(); //Invoke the lambda on behalf of the caller so that we can time the function that was passed in. StopTimer(); LogResultAndTime(result); } catch (Exception ex) { LogExceptionAndTime(ex); } } And all usages of the boilerplate get the updated/enhanced behavior immediately without the need for me to hunt down every instance of the boilerplate and update them by hand. Lambdas can also capture contextual data from the local scope, which allows your boilerplate to ignore implementation details about your lambda, like parameters and dependencies. var myParam = CalculateExpensiveDependency(); Boilerplate(() => DoSomething(myParam)); Boilerplate(() => DoSomethingElse(myParam)); Because we are using lambdas, Boilerplate() doesn't need to know anything about the parameters which DoSomething() or DoSomethingElse() requires. This reduces coupling, and makes your code more resusable, more resilient and more maintainable
🌐
W3Schools
w3schools.com › java › java_lambda.asp
Java Lambda Expressions
A lambda expression can be stored in a variable. The variable's type must be an interface with exactly one method (a functional interface). The lambda must match that method's parameters and return type. Java includes many built-in functional interfaces, such as Consumer (from the java.util package) used with lists.
Discussions

[Java] ELI5 Lambda expressions
Lambda expressions are, for the most part, a more concise way of writing something that previously would have required a class instance (usually an anonymous class) to implement. So if you understand how and why anonymous classes are used, you can figure out lambda expressions. If you don't, then your first step should be to understand those. Java does not have function pointers. Function pointers are used in C and C++ extensively. They are a way of passing functions to other functions. In Lisp you can do that directly, because functions are so-called "first class objects", but in C you have to pass a pointer to some function X and then the function you are calling can call X via the pointer. But, Java doesn't have these. What you do for Java is pass in an object with an implemented method and whatever you are calling will call the method in that class. This is done something like: someObject.foo(new Bonk() { void bonk(String who) { System.out.println("I'm bonking you, " + who); }); This looks a little confusing, but what we are doing is defining a new class (and not giving it a name) and creating an instance of that class at the same time, and then passing that instance to foo. You could do this as something like: class MyBonk implements Bonk { void bonk(String who) { System.out.println("I'm bonking you, " + who); } } MyBonk bonker = new MyBonk(); someObject.foo(bonker); But the first way is much, much terser, right? I love anonymous classes, but I'm a closet LISP programmer, so that would figure. Anyway, lambda functions are a mostly syntactic shortcut for anonymous classes (although there are JVM changes as well, for reasons that I have not investigated). If you have an anonymous class with just one method then it's pretty obvious that the purpose of that class is to call that method. Anonymous functions remove even more of the cruft and you say: someObject.foo(who -> System.out.println("I'm bonking you, " + who); And every LISP programmer in the world says "Wow, we figured this out over half a century ago". More on reddit.com
🌐 r/learnprogramming
12
37
July 14, 2016
Java 8 idioms: Why the perfect lambda expression is just one line
It's funny how much "best practice" material for good FP or OOP programming boils down to the lessons taught first by structured programming. When a clickbaity article like this comes along on a "hot new idea" like Java 8 lambdas, and it boils down to "refactor your code into functions", well... it suddenly makes me feel like we haven't come very far after all. More on reddit.com
🌐 r/java
47
117
August 3, 2017
Introduction to Lambda Expressions in Java
Resources for learning Java · My attempt to explain the need for Lambda Expressions in Java and how they work under the hood - More on reddit.com
🌐 r/learnjava
1
13
August 27, 2017
High level overview of Lambda Expressions in Java 8

Personally, I find it easier to think of lambdas as just syntactic sugar for interfaces with a single abstract method. It's not clear from the post when they can be used (e.g. not for abstract classes). Also, converting a for-loop to use forEach doesn't require a lambda. It's much cleaner with lambdas, but I could see someone getting confused as to when lambdas can/should be used.

More on reddit.com
🌐 r/programming
42
149
December 25, 2016
🌐
Oracle
oracle.com › webfolder › technetwork › tutorials › obe › java › lambda-quickstart › index.html
Java SE 8: Lambda Quick Start
Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a ...
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › lambda expressions in java
Java 8 Lambda Expression (with Examples)
October 1, 2022 - In general programming language, ... of parameters and the body. In Java, a lambda expression is an expression that represents an instance of a functional interface....
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › lambdaexpressions.html
Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
However, when the Java runtime invokes the method printPersonsWithPredicate, it's expecting a data type of Predicate<Person>, so the lambda expression is of this type. The data type that these methods expect is called the target type. To determine the type of a lambda expression, the Java compiler uses the target type of the context or situation in which the lambda expression was found.
🌐
GeeksforGeeks
geeksforgeeks.org › java › lambda-expressions-java-8
Java Lambda Expressions - GeeksforGeeks
Java lambda expressions, introduced in Java 8, allow developers to write concise, functional-style code by representing anonymous functions.
Published   3 weeks ago
Find elsewhere
🌐
Java Concept Of The Day
javaconceptoftheday.com › home › java 8 lambda expressions
Java 8 Lambda Expressions
March 10, 2019 - Java 8 Lambda Expressions can be defined as methods without names i.e anonymous functions. Like methods, they can have parameters, a body, a return type and possible list of exceptions that can be thrown.
🌐
Blueskyworkshop
blueskyworkshop.com › topics › Java-Pages › lambda-expression-basics
Java 8: Lambda Expression Basics
A quick description of Java 8 Lambda expressions and how they can be used to replace anonymous inner classes. Covers the syntax of Lambda expressions and the definition of functional interface.
🌐
Programiz
programiz.com › java-programming › lambda-expression
Java Lambda Expressions (With Examples)
Here, the method does not have any parameters. Hence, the left side of the operator includes an empty parameter. The right side is the lambda body that specifies the action of the lambda expression. In this case, it returns the value 3.1415. In Java, the lambda body is of two types.
🌐
Reddit
reddit.com › r/learnprogramming › [java] eli5 lambda expressions
r/learnprogramming on Reddit: [Java] ELI5 Lambda expressions
July 14, 2016 -

Hey,

I'm learning java right now and I have problems understanding them. I already googled, I read you can use them to return methods. I don't really get it, in which case would you have to return a method and how would you use a returned method?

If someone could explain Lambda expressions to me that would be great!

Top answer
1 of 5
23
Lambda expressions are, for the most part, a more concise way of writing something that previously would have required a class instance (usually an anonymous class) to implement. So if you understand how and why anonymous classes are used, you can figure out lambda expressions. If you don't, then your first step should be to understand those. Java does not have function pointers. Function pointers are used in C and C++ extensively. They are a way of passing functions to other functions. In Lisp you can do that directly, because functions are so-called "first class objects", but in C you have to pass a pointer to some function X and then the function you are calling can call X via the pointer. But, Java doesn't have these. What you do for Java is pass in an object with an implemented method and whatever you are calling will call the method in that class. This is done something like: someObject.foo(new Bonk() { void bonk(String who) { System.out.println("I'm bonking you, " + who); }); This looks a little confusing, but what we are doing is defining a new class (and not giving it a name) and creating an instance of that class at the same time, and then passing that instance to foo. You could do this as something like: class MyBonk implements Bonk { void bonk(String who) { System.out.println("I'm bonking you, " + who); } } MyBonk bonker = new MyBonk(); someObject.foo(bonker); But the first way is much, much terser, right? I love anonymous classes, but I'm a closet LISP programmer, so that would figure. Anyway, lambda functions are a mostly syntactic shortcut for anonymous classes (although there are JVM changes as well, for reasons that I have not investigated). If you have an anonymous class with just one method then it's pretty obvious that the purpose of that class is to call that method. Anonymous functions remove even more of the cruft and you say: someObject.foo(who -> System.out.println("I'm bonking you, " + who); And every LISP programmer in the world says "Wow, we figured this out over half a century ago".
2 of 5
5
In Java there's no such thing as passing a method as an argument to another method, but conceptually sometimes that's really what you would like to do. Traditionally, Java forces you to define a class that contains the method of interest and pass an instance of that class just so that its method gets called. For instance, when you want to sort a list of stuff, you might want to tell the Collections.sort method how to compare the objects while sorting them, but you can't just pass a compare method. So you have to define your special compare method in a special Comparator class just to pass the compare method to the sort method. This is verbose and confusing on the eye. A lambda expression is shorter, simpler syntax to do the same thing. https://ideone.com/8M9OFT Notice the lambda expression itself (a, b) -> Integer.compare(a.length(), b.length()) resembles the args and return statement of the conventional Comparator.compare method @Override public int compare(String a, String b) { return Integer.compare(a.length(), b.length()); } in a cleaner way. Essentially, a lambda is a method without a name and it automagically returns whatever it calculates. This particular lambda behaves the same as a Comparator.compare method.
🌐
Nagarro
nagarro.com › en › blog › post › 26 › lambda-expressions-in-java-8-why-and-how-to-use-them
Lambda Expressions in Java 8: Why and How to Use Them
December 19, 2014 - In Java 8, using lambda expression and Stream API we can pass processing logic of elements into methods provided by collections and now collection is responsible for parallel processing of elements and not the client.
🌐
Wordpress
rodrigouchoa.wordpress.com › 2014 › 09 › 10 › java-8-lambda-expressions-tutorial
Java 8 Lambda Expressions Tutorial | Code to live. Live to code.
March 21, 2016 - To summarize it, lambda expressions are going to allow passing of behavior, functions, as arguments in a method call. It’s a paradigm a little different from which java programmers are used to, since all this time we have only written methods that take objects as parameters, not another methods!
🌐
w3resource
w3resource.com › java-exercises › lambda › index.php
Java Lambda Expressions - Exercises, Practice, Solution
May 9, 2025 - A lambda expression is just a shorter way of writing an implementation of a method for later execution. ... Write a Java program to implement a lambda expression to find the sum of two integers.
🌐
Medium
medium.com › @mesfandiari77 › understanding-lambda-expressions-in-java-c01d20c42ee9
Understanding Lambda Expressions in Java | by MEsfandiari | Medium
June 27, 2023 - Lambda expressions are a powerful feature of Java 8 that enable functional programming style. They provide concise and expressive ways to represent anonymous functions, which can be used in a variety of contexts.
🌐
Medium
medium.com › @AlexanderObregon › how-javas-lambda-expressions-work-under-the-hood-8f783ae37928
How Java’s Lambda Expressions Work Under the Hood | Medium
February 25, 2025 - Instead of generating an anonymous inner class, Java creates a dynamic method call that is resolved at runtime. Lambda expressions in Java do not work the same way as traditional anonymous inner classes. Instead of generating a separate class file, the compiler uses a mechanism that allows function instances to be created dynamically at runtime.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-lambdas-in-java
How to Use Lambdas in Java | DigitalOcean
February 28, 2024 - In this tutorial, you will learn to write your lambda expressions. You will also learn to use some built-in Lambdas available in the java.util.function package.
🌐
Medium
abu-talha.medium.com › lambda-expressions-in-java-a-concise-guide-with-examples-47c7ade952fb
Lambda Expressions in Java: A Concise Guide with Examples | by Abu Talha | Medium
October 8, 2023 - A lambda expression is a concise way to represent an anonymous function (a function without a name) in Java.
🌐
Baeldung
baeldung.com › home › java › lambda expressions and functional interfaces: tips and best practices
Lambda Expressions and Functional Interfaces: Tips and Best Practices | Baeldung
December 16, 2023 - But in the case of mutable object variables, a state could be changed inside lambda expressions. ... This code is legal, as total variable remains “effectively final,” but will the object it references have the same state after execution of the lambda? No! Keep this example as a reminder to avoid code that can cause unexpected mutations. In this article, we explored some of the best practices and pitfalls in Java 8’s lambda expressions and functional interfaces.
🌐
DEV Community
dev.to › maddy › java-8-lambda-expression-3egf
Java 8: Lambda Expression - DEV Community
January 3, 2022 - The syntax for a lambda expression is parameter → expression.
🌐
Board Infinity
boardinfinity.com › blog › lambda-expression-in-java
Lambda Expression in Java | Board Infinity
August 14, 2025 - When needed lambda expression can be used as an object and can also be passed around. Not being in any class, a function can be created. Also gives the implementation of a functional interface. Java lambda expression has three components.