DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Trending

  • Solid Testing Strategies for Salesforce Releases
  • Internal Developer Portals: Modern DevOps's Missing Piece
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  1. DZone
  2. Coding
  3. Java
  4. Java 8: Lambda Functions—Usage and Examples

Java 8: Lambda Functions—Usage and Examples

One of the major new language features in Java 8 is the lambda function. In fact, it is one of the biggest changes since the Java 1 release. Take a look at this introduction to Java 8's lambda function.

By 
Hari Kiran G user avatar
Hari Kiran G
·
May. 04, 16 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
213.4K Views

Join the DZone community and get the full member experience.

Join For Free

One of the major new language features in Java 8 is the lambda function. In fact, it is one of the biggest changes since the Java 1 release. Lambdas are widely used in the programming language world, including the languages that compile to the Java platform. For instance, Groovy compiles to the Java platform and has very good support for lambda functions (also known as closures). Oracle decided to bring lambdas to the mainstream language on the JVM—the Java language itself—with Java 8.

Lambda Function Related Changes in Java 8

The introduction of lambdas required coordinated changes in the language, library, and the VM implementation:

  • The arrow operator (“->”) for defining lambda functions, the double colon operator (“::”) used for method references, and the default keyword.

  • The streams library and the integration of the collections library with streams.

  • Lambda functions are implemented using the invokedynamic instruction introduced in Java 7.

To support the introduction of lambdas into the language, the type inference has also been strengthened in Java 8. Lambdas enabled library writers to create parallel algorithms in the library to exploit inherent parallelism in the modern hardware (i.e., multi-cores).

In Java 8, java.util has been considerably enhanced using lambda functions. Java 8 has added two new packages: java.util.function and java.util.streams. 

Lambdas can significantly change the way you design and write code. Why? Lambdas support the functional programming paradigm—that means learning and using lambdas would mean a paradigm shift for you. But you don’t need to worry about making a major shift—Java seamlessly integrates functional capabilities with the existing object-oriented features, and you can gradually shift to using more and more functional features in your programs.

Lambda Functions: Syntax

A lambda function consists of optional parameters, the arrow token, and the body:

LambdaParameters -> LambdaBody

  • LambdaParameters are parameters to the lambda function passed within opening parenthesis "(" and closing parenthesis ")". When more than one parameter is passed, they are separated by commas.

  • To support lambdas, Java has introduced a new operator “->”, also known as lambda operator or arrow operator. This arrow operator is required because we need to syntactically separate the parameter from the body. 

  • LambdaBody can be an expression or a block. The body could consist of single statement (in that case no explicit curly braces defining a block are required); such a lambda body is known as  an "expression lambda." If there are many statements in a lambda body, they need to be in a block of code; such a lambda body is known as "block lambda."

The compiler performs type inference for lambda expressions:

  • The compiler infers the types of the parameters if you do not specify the parameter types in a lambda function definition. When you specify the type of parameters, you need to either specify all or none, or else you will get a compiler error.

  • You can omit the parenthesis if there is only one parameter. But in this case, you cannot provide the type explicitly. You should leave it to the compiler to infer the type of that single parameter.

  • The return type of the lambda function is inferred from the body. If any of the code in the lambda returns a value, then all the paths should return a value, or else you will get a compiler error.

Some examples of valid lambda expressions (assuming that relevant functional interfaces are available):

(int x) -> x + x

x -> x % x

() -> 7

(int arg1, int arg2) -> (arg1 + arg2) / (arg1 – arg2)


Examples of invalid lambda expressions:

 -> 7 

// if there are no parameters, then empty parentheses () must be provided

(arg1, int arg2) -> arg1 / arg2 

// if argument types are provided, then it should be provided for all arguments or none of them


Lambda Function—An Example

Let us get started with a simple “hello world” example for lambda functions:

interface LambdaFunction {
    void call();
}

class FirstLambda {
    public static void main(String []args) {
        LambdaFunction lambdaFunction = () -> System.out.println("Hello world");
        lambdaFunction.call();
    }
}

When executed, this program prints:

Hello world

In this program, the interface LambdaFunction declares an abstract method named call(); hence it is a functional interface. Inside the main method in the FirstLambda class, a lambda function is assigned to a variable of the functional interface type LambdaFunction.

LambdaFunction lambdaFunction = () -> System.out.println("Hello world");


Here, the expression () -> System.out.println("Hello world") is a lambda expression:

  • The syntax () indicates no parameters.

  • The arrow operator "->" separates method parameters from the lambda body.

  • The statement System.out.println("Hello world") is the body of the lambda expression.

How does the lambda expression relate to the functional interface LambdaFunction? It is through the single abstract method inside the LambdaFunction interface: void call(). The signature of this abstract method and the lambda expression must match:

  • The lambda expression has () indicating it has no parameters—it matches with the call method that takes no parameters.

  • The statement System.out.println("Hello world") is the body of the lambda expression. This body serves as an implementation of the lambda function. 

  • There is no return statement in this lambda expression body and hence the compiler infers the return type of this expression as the void type—that matches with the return type of the call method.

The next statement lambdaFunction.call(); invokes the lambda function. As a result of this function call, “Hello world” is printed on the console. Check out this book for more learning about Lambda Functions. You can download the source code here.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!