Java 8 - Functions

Java 8 - Functions

As we already studied, Java 8 introduced us the Functional Interface. It's great because we can work with Lambda Expressions and we can get our code easier to read and if you are a brave developer, you can write your code in a functional style.

But there is more! Java 8 contains many built-in functional interfaces to get our life great! Next you can see a list of a few built-in Functional Interfaces:

  • Predicates
  • Functions
  • Suppliers
  • Consumers
  • Comparators

In this part of the Java 8 Guide we going to see about the Function functional interface

Step 1 - Presenting you the Function Functional Interface

Function interface is a great built-in functional interface that has a few methods to be implemented and a few default method to facilitate many operations.

The main goal of this interface is :

  • Get an argument and
  • Transform it it to another one as a produced result.

Example: Imagine that you have a number as String and you need to convert it to an Integer

Like functions in mathematics, we will apply some rule to a given value.

So, show me the code!

Step 2 - Converting a String using Function Interface

In this example, we will convert a String by using Function Interface, but in a vintage style, without using lambda expression:

As you can see, we implemented the apply(T t) method from the Function interface by using an anonymous class. Notice that the apply method is receiving an argument to be transformed in another result.

Step 3 - Converting a String using Function Interface and Lambda Expression

Now it's a great time to leave the vintage style and use Lambda Expressions \o/

Let's see the code!

Great! Functions and Lambda Expressions can make our code really easier to read and understand!

Step 5 - Composing Functions without Lambda Expression

Sometimes, like functions in mathematics, we should compose our functions with another given functions. Let's see the example:

Example - Imagine that:

  • You need to convert a String number into an Integer
  • You need to multiply the result by 10.

Pretty simple and easy to follow. Let's see the code in a vintage style, without lambdas:

As you can see, we used the andThen() method from Function interface. Notice that this is a default method to make our life easier.

So, in this example, the following steps will be made:

  • Converts the string number into integer, that results in 10
  • Multiplies the result by 10, resulting in 100

Let's see more code, at this time by using lambda expressions

Step 6 - Converting a String using Function Interface

In this final example, let's resolve the same problem by using Function interface and Lambda Expression:

Pretty easier to read!

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

Thanks!

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: