Java 8 - Lambda Expression

Java 8 - Lambda Expression

Hey! In this part of the Java 8 Series, we're going to see Java 8 Lambda Expressions!

Our examples will go through 2 really useful Interfaces/Classes in Java: Runnable and Collections.

Are you ready?

Step 1 - Lambda Expression using Methods without Parameters

In the following example, we will print a message in another Thread using Runnable interface. To do that, it is really common to create an anonymous class that will implements the run() method from Interface.

Show me the code!

Great! Everything is working as we expected!

I know, pretty simple code BUT with a few boilerplate codes. With Java 8 Lambda Expression we can write the same code by using lambda and remove all the unnecessary.

But which code is unnecessary here?

Let's think in another way? Which code is necessary here?

Hmmm...

Yes! Just that. I don't care about the name of the method, I don't care about the @Override annotation, I don't care about the world. I really would like to JUST print a message.

Let's begin our journey to Lambda expression in the following code:

Awesome! As you can see the code is much easy to understand and really shorter!

Notice that we changed the entirely method by using () ->

Let go a little bit deeper

Step 2 - Lambda Expression using Method with Parameters

Now let's see another great example of how to use Lambda Expression, by this time using a method that should receive a parameter.

The first code will use the common approach, creating an anonymous class

Again, the necessary code is just

So then, let's remove the method name and just use its signature

Notice that the signature is using the arrow

But we would like to remove more boilerplate code. It's time to remove braces {} and the return keyword

Great! But wait! Java 8 improved its Type Inference in parameters. Let's remove them:

Awesome! Now, the final code could be:

That's it! I hope that this Java 8 Tutorial would be useful to you! Thanks!

Follow us to keep up to date! \o/

Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
Java 8 - Lambda Expression - Java Tutorial Hacking Code

Java 8 - Lambda Expression

Do you know how to use Java 8 Lambda Expressions? No? Let's see it!
Java 8 - Default Methods - Java Tutorial Hacking Code

Java 8 - Default Methods Tutorial

Did you know that now we can have non-abstract methods in Interfaces? Let's see why!
Java 8 - Functions - Java Tutorial Hacking Code

Java 8 - Functions

Do you want to try a more Functional Programming Style with Functions?
Java 8 - Method Reference - Java Tutorial Hacking Code

Java 8 - Method Reference

Method Reference is a really awesome feature from Java 8! Let's see why and how to use it!
Java 8 - Stream Count - Java Tutorial Hacking Code

Java 8 - Stream Match Tutorial

Stop suffering and let's use Java 8 Stream Count!
Java 8 - Stream Filter - Java Tutorial Hacking Code

Java 8 - Stream Filter

Filtering data with Stream Filter from Java 8
Java 8 - Stream Map - Java Tutorial Hacking Code

Java 8 - Stream Map

Updating data with Stream Map from Java 8
Java 8 - Stream Sorted - Java Tutorial Hacking Code

Java 8 - Stream Sorted

Sorted Collections with Java 8 Stream Sorted
Java 8 - Stream Match - Java Tutorial Hacking Code

Java 8 - Stream Match Tutorial

Let's see if any data Match with rules using Stream Match from Java 8!
Java 8 Stream Reduce Tutorial - Java Guide by Live Coding

Java 8 - Stream Reduce Tutorial

Let's move on and reduce some data with Stream Reduce from Java8!
Java 8 Consumer Tutorial - Java Guide by Live Coding

Java 8 - Consumers Tutorial

Java 8 Consumers - Making our life easier!
Java 8 Predicate Tutorial - Java Guide by Live Coding

Java 8 - Predicate Tutorial

Java 8 Predicate - An important built-in Functional Interface!
Java 8 Comparator Tutorial - Java Guide by Live Coding

Java 8 - Comparator Tutorial

Java 8 Comparator is known from old times...Let's see what has changed!
Java 8 Supplier Tutorial - Java Guide by Live Coding

Java 8 - Supplier Tutorial

Java 8 Suppliers become an easy Functional Interface to like!

0 Comments

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: