Over a million developers have joined DZone.

Java 8: Lambda Expressions

DZone's Guide to

Java 8: Lambda Expressions

One of the more exciting features of Java 8 is the introduction of lambda expressions. Check out a quick sample and a summary of other exciting changes.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

The latest version of Java Platform Standard Edition 8 contains a major feature release. This significant enhancement is, of course, lambda expressions.

Lambda Expressions

By definition, lambda expressions are anonymous functions given as constant values. They may appear anywhere, just like a constant, but typically, they are passed as parameters to another function. They execute in the context of their appearance and, hence, they can use the values of the variables that are defined in their context.

Let's take a look at the below example, wherein we are printing the values in a List containing string using an enhanced for loop:

public class LambdaExpressions{
    public static void main(String[] args) {
        List<String> stringList = Arrays.asList("String1","String2","String3");

        for(String str : stringList) {

Now, let's try to do the same job using lambda expressions, which is a more optimized approach:

public class LambdaExpressions {
    public static void main(String[] args) {
        List<String> stringList = Arrays.asList("String1","String2","String3");

        stringList.forEach(stringListValue -> System.out.println(stringListValue));

The structure has many similarities to a function. In parentheses, we set up a list of arguments. The syntax -> shows that this is a lambda. And in the right-hand part of this expression, we set up the behavior of our lambda.

Other interesting features of Java 8 include:

  • Parallel Operations using Stream API
    One interesting feature of the new Stream API is that a Stream can be parallel — the operations are automatically done in parallel. No explicit multi-threading code is required. The methods are stream() and parallelStream(), which are default methods and have been written in Collection interface. The parallel operations improve concurrency.

  • New Date and Time APIs
    Until Java 7, the date and time APIs lacked thread safety, good design (for example, fewer direct methods for date operations), and it was difficult to handle different time zones. Java 8 has introduced a new date-time API under the package java.time. The following are some of the important classes introduced in java.time package:

    • Local: Simplified date-time API with no complexity of time zone handling. 

    • Zoned: Specialized date-time API to deal with various time zones.

  • Default method implementation for interfaces
    Default methods in Java 8 can be considered as a form of multiple inheritance. You can have a default method implementation for an interface. And when you define a class implementing multiple interfaces, you can achieve a behavior similar to multiple inheritance, which was not supported by previous Java versions.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

java ,lambda expressions ,java 8 ,api

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}