Core Java

Java 8 Lambda Expressions Tutorial

Photo of Ima MiriIma MiriOctober 23rd, 2015Last Updated: February 25th, 2019
0 490 4 minutes read

Lambda expressions are an important new feature in Java 8 which provide a clear and concise way to represent a functional interface using an expression. They also improve some functionality in Collection libraries such as iterate, filter and extracting data from a Collection.
 
 
 
 
 
 
 
 

1. Lambda Expression Syntax

A lambda expression is included three parts.

Argument ListArrow TokenBody
(int x, int y)->x + y

 

  • The argument can be zero, one or more statement in lambda expressions. The type of parameter can be declared or can be inferred from the context. This is called “target typing”.
  • The body can be either a single expression or a statement block. In the expression form, the body is simply evaluated and returned. In the block form, the body is evaluated like a method body and a return statement returns control to the caller of the anonymous method.

Lets take a look at these examples:

() -> 33(String str) -> { System.out.println(str); }(int x, int y) -> x + y

The first expression takes no arguments and uses the expression form to return an integer 33. The second expression takes a string and uses the block form to print the string to the console, and returns nothing. The third expression takes two integer arguments, namedx andy and uses the expression form to returnx+y.

2. Why Lambda expressions?

In Java, anonymous inner classes provide a way to implement classes that may occur only once in an application. Lambda expressions are a useful replacement for anonymous inner classes to simplify the code.

In the following example, theRunnable lambda expression, converts five lines of the code into one line.

public static void main(String[] args) {    Runnable ar = new Runnable(){        @Override        public void run(){            System.out.println("Anonymous Runnable!");        }    };    Runnable lr = () -> System.out.println("Lambda Runnable!");    ar.run();    lr.run();}

Lets have a look at the following example for using comparator interface and how we can replace part of the code with lambda expressions:

List<Node> nodes = new ArrayList<Node>();// Sort with Inner ClassCollections.sort(nodes, new Comparator<Node>() {    public int compare(Node n1, Node n2) {        return n1.getValue().compareTo(n2.getValue());    }});System.out.println("=== Sorted Ascending ===");for(Node node:nodes){    node.toString();}// Replace with lambdaExpressionSystem.out.println("=== Sorted Ascending ===");Collections.sort(nodes, (Node n1, Node n2) -> n1.getValue().compareTo(n2.getValue()));for(Node node:nodes){    node.toString();}System.out.println("=== Sorted Descending ===");Collections.sort(nodes, (n1, n2) -> n2.getValue().compareTo(n1.getValue()));for(Node node:nodes){    node.toString();}

The first lambda expression in line 17 declares the parameter type passed to the expression. However, as you can see from the second expression in line 24, this is optional. Lambda supports “target typing” which infers the object type from the context in which it is used. Because we are assigning the result to a Comparator defined with a generic, the compiler can infer that the two parameters are of theNode type.

2.1. How to define a functional interface

A Functional Interface includes only an abstract method. A lambda expression can be implicitly assigned to a functional interface as bellow.

@FunctionalInterfacepublic interface Print {    public abstract void print();}

Functional Interfaces can have only one abstract method. If you try to add one more abstract method in it, it throws compile time error.

Error: java: Unexpected @FunctionalInterface annotation  main.Print is not a functional interface    multiple non-overriding abstract methods found in interface main.Print

Once the Functional interface is defined, we can simply use it in our development.

public static void main(String[] args) {   // Functional Interface   Print p = () -> System.out.println("Test");   p.print();}

3. The java.util.function package

There are number of different functional interfaces injava.util.function package. Predicate is one of the functional interface in this package which includes a test method. The method takes a generic class and returns a boolean result.Predicate is useful for filtering mechanisms.

Want to be a Java 8 Ninja ?
Subscribe to our newsletter and download the Java 8 FeaturesUltimateGuideright now!
In order to get you up to speed with the major Java 8 release, we have compiled a kick-ass guide with all the new features and goodies! Besides studying them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

public interface Predicate<T> {    boolean test(T t);}

The following example uses the Predicate interface to compare the distance between two cities with a constant value. As it is mentioned bellow, it will print the distance if it is greater than 30.

public static void main(String[] args) {   List<Edge> cities = new ArrayList<>();   cities.add(new Edge(new Node("LA"), new Node("NY"), 50));   cities.add(new Edge(new Node("Sydney"), new Node("Melbourne"), 30));   cities.add(new Edge(new Node("Rome"), new Node("Venice"), 20));   Predicate<Edge> edgePredicate = e -> e.getDist() >= 30;    filter(edgePredicate, cities); } public static void filter(Predicate<Edge> predicate, List<Edge> items) {    for(Edge item: items) {       if(predicate.test(item)) {          System.out.println(item.getOrigin().getValue().toString() + " to " + item.getDest().getValue().toString() + " : " + item.getDist());      }    } }

Output:

LA to NY : 50Sydney to Melbourne : 30

4. The java.util.stream package

In java,java.util.Stream represents a stream which one or more operations can be performed. AStream represents a sequence of elements on which various methods can be chained. By default, once elements are consumed they are no longer available from the stream. Therefore, a chain of operations can occur only once on a particularStream.

Now, lets have a look at some examples with stream.The first feature to look at is the newforEach method available to any collection class. forEach is a new addition toIterable interface in java 8.

List<String> myList =        Arrays.asList("area", "block", "building", "city", "country");myList.stream()        .filter(s -> s.startsWith("c"))        .map(String::toUpperCase)        .sorted()        .forEach(System.out::println);

Output:

CITYCOUNTRY

Stream operations are either intermediate or terminal. Intermediate operations return a stream so we can chain multiple intermediate operations without using semicolons. Terminal operations are either void or return a non-stream result. In the above examplefilter,map andsorted are intermediate operations whereasforEach is a terminal operation. The above example shows a chain of stream operations which is known as “operation pipeline”.

5. Download the Source Code

This was a tutorial for Java 8 Lambda expressions.

Download
You can download the full source code of this example here: Java 8 Lambda Expressions Tutorial
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Ima MiriIma MiriOctober 23rd, 2015Last Updated: February 25th, 2019
0 490 4 minutes read
Photo of Ima Miri

Ima Miri

Ima is a Senior Software Developer in enterprise application design and development. She is experienced in high traffic websites for e-commerce, media and financial services. She is interested in new technologies and innovation area along with technical writing. Her main focus is on web architecture, web technologies, java/j2ee, Open source and mobile development for android.

Related Articles

Bipartite Graph

Java not equal Example

January 17th, 2020
Bipartite Graph

Java API Tutorial

October 26th, 2020
Bipartite Graph

Java Struct Example

January 8th, 2020
Bipartite Graph

Java Node Example

November 20th, 2019
Bipartite Graph

Java Swing MVC Example

January 26th, 2016
Bipartite Graph

How to call a method in Java

December 26th, 2019
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.